我想第三列“项目”与分组的值.
Dictionary<string,int> dic = new Dictionary<string,int>(); dic.Add("a",1); dic.Add("b",1); dic.Add("c",2); dic.Add("d",3); var dCounts = (from i in dic group i by i.Value into g select new { g.Key,count = g.Count()}); var a = dCounts.Where(c => c.count>1 ); dCounts.Dump(); a.Dump();
此代码导致:
Key Count 1 2 2 1 3 1
我想要这些结果:
Key Count Items 1 2 a,b 2 1 c 3 1 d
解决方法
var dCounts = (from i in dic group i by i.Value into g select new { g.Key,count = g.Count(),Items = string.Join(",",g.Select(kvp => kvp.Key)) });
使用string.Join(“,”,{array}),传入您的数组键.