-2
我有這樣的代碼片段:LINQ GroupBy - 如何分組?
List<string[]> list = new List<string[]>();
list.Add(new string[] { "A", "q", "t" });
list.Add(new string[] { "B", "w", "t" });
list.Add(new string[] { "B", "e", "r" });
list.Add(new string[] { "A", "r", "t" });
list.Add(new string[] { "B", "t", "e" });
var result = from item in list
group item by item[0] into g
let count = g.Count()
orderby count descending
select new { Value = g.Key, Count = count};
foreach (var res in result)
{
Console.WriteLine("Value: " + res.Value + " Count: " + res.Count);
}
該產品這樣的結果:
Value: B Count: 3
Value: A Count: 2
我怎樣才能得到這樣的結果,沒有太多的foreach?
Values: A q t - Count 2
Values: A r t - Count 2
Values: B w t - Count 3
Values: B e r - Count 3
Values: B t e - Count 3
你怎麼有計數'2'爲'A Q t'?同樣用你提供的數據計算其他人的數值? –
計數2是因爲字母「A」在數據的第一列中出現兩次。字母「B」出現三次。 –