c# – 从同一列表中选择相似的元素
发布时间:2020-12-16 01:56:18 所属栏目:百科 来源:网络整理
导读:我有一个文件,其中包含从Excel工作表中解析的信息: class ProfileRecord{ public string ProfileCode { get; set; } public string DID { get; set; } public string AppName { get; set; } public string lvl1 { get; set; } public string lvl2 { get; se
我有一个文件,其中包含从Excel工作表中解析的信息:
class ProfileRecord { public string ProfileCode { get; set; } public string DID { get; set; } public string AppName { get; set; } public string lvl1 { get; set; } public string lvl2 { get; set; } public string lvl3 { get; set; } public string lvl4 { get; set; } } 在另一个班级中,我发送这些记录的列表进行分析.我想做的一件事是创建一个var,找到所有匹配的ProfileCodes.所以,excel表的一部分是: Name Profile Code Billy 134 Sally 156 Mary 134 Jimmy 134 Tom 189 然后我会有一些东西(我真的不知道): var sameProfile = from a in profileList where ( //some code to group all similar profile codes) select new { name = a.Name }; 如果我打印: foreach(var p in sameProfile) { Console.WriteLine(p.name); Console.WriteLine("n"); } 我希望有: Billy Mary Jimmy Sally Tom 打印.我不确定如何找到所有simialr元素并使用LINQ对它们进行分组.建议? 解决方法
使用Enumerable.GroupBy:
var profileCodeGroups = profileList .GroupBy(pr => pr.ProfileCode) .OrderBy(g => g.Key); foreach(var grp in profileCodeGroups) { foreach(var pr in grp) Console.WriteLine(pr.Name); Console.WriteLine(""); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |