问题描述
我有以下三个类;
public class City { public int CityId { get; set; } public Region Region { get; set; } public string Name { get; set; } } public class Region { public int RegionId { get; set; } public Country Country { get; set; } public string Name { get; set; } } public class Country { public string CountryCode { get; set; } public string Name { get; set; } }
我已经填充了一个城市列表对象,以包含许多城市,每个城市都有一个地区和一个国家.
现在,我想获得所有城市所有国家的清单.我尝试了以下;
List<City> CityObjectList = GetAllCity(); CityObjectList.Select(r => r.Region).ToList().Select(c => c.Country).ToList();
但是,我回来的只是所有国家.我如何获得不同的国家?
推荐答案
您可以使用:
var allCityCountries = CityObjectList.Select(c => c.Region.Country).ToList();
此列表并非不同.为了使国家独特,您可以覆盖Equals + GetHashCode在Country中,对Enumerable.Disinct实施自定义IEqualityComparer<Country>或使用GroupBy(最慢但最简单的选项):
var distinctCountries = CityObjectList .Select(c => c.Region.Country) .GroupBy(c => c.CountryCode) .Select(g => g.First()) .ToList();
IEqualityComparer<T>方法:
class CountryCodeComparer : IEqualityComparer<Country> { public bool Equals(Country x, Country y) { if(object.ReferenceEquals(x, y)) return true; if(x == null || y == null) return false; return x.CountryCode == y.CountryCode; } public int GetHashCode(Country obj) { return obj == null ? 0 : obj.CountryCode == null ? 0 : obj.CountryCode.GetHashCode(); } }
现在您可以使用Distinct与它的实例:
var comparer = new CountryCodeComparer(); distinctCountries = CityObjectList.Select(c => c.Region.Country).Distinct(comparer).ToList();
问题描述
I have the following three classes;
public class City { public int CityId { get; set; } public Region Region { get; set; } public string Name { get; set; } } public class Region { public int RegionId { get; set; } public Country Country { get; set; } public string Name { get; set; } } public class Country { public string CountryCode { get; set; } public string Name { get; set; } }
I have populated a City List object to contain a number of cities, of which each have a Region and a Country.
Now I want to get a list of all countries for all cities. I've tried the following;
List<City> CityObjectList = GetAllCity(); CityObjectList.Select(r => r.Region).ToList().Select(c => c.Country).ToList();
However, all I get back is all the countries. How can I get the distinct countries ?
推荐答案
You can use:
var allCityCountries = CityObjectList.Select(c => c.Region.Country).ToList();
This list is not distinct. To make the countries unique you could either override Equals + GetHashCode in Country, implement a custom IEqualityComparer<Country> for Enumerable.Disinct or use GroupBy(slowest but easiest option):
var distinctCountries = CityObjectList .Select(c => c.Region.Country) .GroupBy(c => c.CountryCode) .Select(g => g.First()) .ToList();
The IEqualityComparer<T> way:
class CountryCodeComparer : IEqualityComparer<Country> { public bool Equals(Country x, Country y) { if(object.ReferenceEquals(x, y)) return true; if(x == null || y == null) return false; return x.CountryCode == y.CountryCode; } public int GetHashCode(Country obj) { return obj == null ? 0 : obj.CountryCode == null ? 0 : obj.CountryCode.GetHashCode(); } }
Now you can use Distinct with an instance of it:
var comparer = new CountryCodeComparer(); distinctCountries = CityObjectList.Select(c => c.Region.Country).Distinct(comparer).ToList();