gpt4 book ai didi

c# - LINQ 查询(分组依据)?

转载 作者:行者123 更新时间:2023-11-30 13:28:36 24 4
gpt4 key购买 nike

考虑以下对象:

public class Address { public string city; public string state; public string country; }

如果我有一个地址列表,我将如何使用 LINQ 获取城市、州和国家匹配的计数列表。

所以我的结果可能是这样的:

  • "普林斯顿""nj""美国"122
  • “奥斯汀”“TX”“美国”44
  • “la” “ca” “美国” 1
  • “普林斯顿”“na”“英国”3
  • ....

谢谢!

最佳答案

比 Marc 的回答更进了一步(他在我发布之前编辑了!)。哈哈

var qry = from addr in addresses
group addr by new { addr.city, addr.state, addr.country } into grp
select new
{
city = grp.Key.city,
state = grp.Key.state,
country = grp.Key.country,
count = grp.Count(),
};

关于c# - LINQ 查询(分组依据)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4918754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com