[英]Count, orderby and group in LINQ
Given a set 给定一套
Paris New York London New York Paris Paris 巴黎纽约伦敦纽约巴黎巴黎
I'm trying to do a LINQ query that will return a grouping of the Paris, New York, and London with their respective counts . 我正在尝试执行LINQ查询,该查询将返回巴黎,纽约和伦敦及其各自数量的分组。
I've been able to do a GroupBy that returns a group containing only the Paris/New York/London in each Grouping. 我已经能够做一个GroupBy,它返回每个分组中仅包含巴黎/纽约/伦敦的一组。
Sounds relatively simple, but I can't get it to work? 听起来相对简单,但我无法使它正常工作?
Edit : It's not a homework problem. 编辑 :这不是一个作业问题。
CODE 码
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);
}
OUTPUT 输出值
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.