LINQ中的Count,orderby和group(Count, orderby and group in LINQ)

系统教程 行业动态 更新时间:2024-06-14 16:59:46
LINQ中的Count,orderby和group(Count, orderby and group in LINQ)

给定一套

巴黎纽约伦敦纽约巴黎巴黎

我正在尝试做一个LINQ查询,该查询将返回巴黎,纽约和伦敦的分组及其各自的计数

我已经能够在每个分组中做一个返回仅包含巴黎/纽约/伦敦的组的GroupBy。

听起来比较简单,但我不能让它起作用?

编辑 :这不是一个家庭作业问题。

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.

I've been able to do a GroupBy that returns a group containing only the Paris/New York/London in each Grouping.

Sounds relatively simple, but I can't get it to work?

Edit: It's not a homework problem.

最满意答案

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).

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).

更多推荐

本文发布于:2023-04-17 08:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/c0fedbe0f56ea8d7b8c2f837a3c8724e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Count   LINQ   group   orderby

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!