LINQ查询(Group BY)?

Luk*_*ina 3 c# linq

考虑以下对象:

public class Address { public string city; public string state; public string country; }
Run Code Online (Sandbox Code Playgroud)

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

所以我的结果看起来像这样:

  • "princeton""nj""usa"122
  • "奥斯汀""tx""美国"44
  • "la""ca""usa"1
  • "princton""na""uk"3
  • ....

谢谢!

Eni*_*ity 9

比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(),
          };
Run Code Online (Sandbox Code Playgroud)