[英]Count, orderby and group in LINQ
給定一套
巴黎紐約倫敦紐約巴黎巴黎
我正在嘗試執行LINQ查詢,該查詢將返回巴黎,紐約和倫敦及其各自數量的分組。
我已經能夠做一個GroupBy,它返回每個分組中僅包含巴黎/紐約/倫敦的一組。
聽起來相對簡單,但我無法使它正常工作?
編輯 :這不是一個作業問題。
碼
String[] input = new [] { "Paris", "New York", "London",
"New York", "Paris", "Paris" };
var groups = input
.GroupBy(city => city)
.Select(group => new { City = group.Key, Count = group.Count() });
foreach(var group in groups)
{
Console.WriteLine("{0} occurs {1} time(s).", group.City, group.Count);
}
輸出值
Paris occurs 3 time(s). New York occurs 2 time(s). London occurs 1 time(s).
在分組上使用.Count()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.