复杂的 LINQ 查询
考虑集合
List<Person> people = new List<Person>
{
new Person{Name = "A", SSN="1", Age = 23},
new Person{Name = "A", SSN="2", Age = 23},
new Person{Name = "B", SSN="3", Age = 24},
new Person{Name = "C", SSN="4", Age = 24},
new Person{Name = "D", SSN="5", Age = 23}
};
问题是:如何编写一个 LINQ 查询来按年龄对人员进行分组,然后计算每个组中具有相同名称的人员数量?
我尝试使用 group by 运算符、嵌套查询所有可能性仍然无法找出确切的查询。
问候, 哎呀
Consider the collection
List<Person> people = new List<Person>
{
new Person{Name = "A", SSN="1", Age = 23},
new Person{Name = "A", SSN="2", Age = 23},
new Person{Name = "B", SSN="3", Age = 24},
new Person{Name = "C", SSN="4", Age = 24},
new Person{Name = "D", SSN="5", Age = 23}
};
The question is: How can I write a LINQ query to group Person on Age and then count number of person with in each group having the same name?
I tried using group by operator, nested queries all possibilities still could not figure out the exact query.
Regards,
Jeez
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
。
Names 属性将名称作为 Key,然后您可以获取每个名称的
Count()
How about
The Names property will have the name as the Key and you can then get
Count()
for each name.基本上与:
吉姆相同
basically the same as:
jim
这与按年龄和姓名分组没有什么不同,包括计数以及可选地按年龄和姓名排序。对对的 LINQ 操作通常可以通过简单地使用匿名类型来完成,如下所示:
这里的唯一键非常清楚,并且可以更改它,而无需更改其余程序的结构。
This is no different from grouping by both age and name, including count and optionally order by age and name. LINQ operations on pairs can usually be done by simply using anonymous types like so:
It's very clear what the unique key is here and it can be changed without having to also change the structure of the remaining program.