1

鉴于以下情况,我希望能够根据两个字段提取非不同的值:名称和国家/地区。在这个例子中,我想返回列表中的前两个。

List<City> cities = new List<City>
{
new City{ Name = "Sydney", Country = "Australia", Galaxy = "Mine"  },
new City{ Name = "Sydney", Country = "Australia", Galaxy = "Yours" },
new City{ Name = "New York", Country = "USA", Galaxy = "Ours"},
new City{ Name = "Paris", Country = "France", Galaxy = "Theirs" },

};
4

1 回答 1

3

此查询将按名称和国家/地区返回不明确的城市(即在您的情况下为前两个):

IEnumerable<City> query = 
    cities.GroupBy(c => new { c.Name, c.Country })
          .Where(g => g.Count() > 1)
          .SelectMany(g => g);
于 2013-01-03T23:33:09.290 回答