Using LINQ to return a list from comparing two lists, one of properties with default values
I have defined my StateRegions properties with default values e.g.:
public string Vermont
{
get
{
return string.IsNullOrEmpty(_region) ? "NorthEast" : _region;
}
set { _region = value; }
}
public string NewHampshire
{
get
{
return string.IsNullOrEmpty(_region) ? "NorthEast" : _region;
}
set { _region = value; }
}
public string Massachusetts
{
get
{
return string.IsNullOrEmpty(_region) ? "NorthEast" : _region;
}
set { _region = value; }
}
public string RhodeIsland
{
get
{
return string.IsNullOrEmpty(_region) ? "NorthEast" : _region;
}
set { _region = value; }
}
public string NewYork
{
get
{
return string.IsNullOrEmpty(_region) ? "NorthEast" : _region;
}
set { _region = value; }
}
public string Pennsylvania
{
get
{
return string.IsNullOrEmpty(_region) ? "NorthEast" : _region;
}
set { _region = value; }
}
Now I want to build a list of regions based on the states in the DB. This is the basic code/pseudocode:
// 1) What states have customers?
List<string> states = GetStatesHavingCustomers();
// 2) What are the regions that correspond to US States?
List<StateRegions> regions = new List<StateRegions>();
// Lets get the get the states in the DB an开发者_如何学运维d
// put the corresponding regions in a list
Any ideas how to do this in LINQ?
I don't quite understand the stateRegion string properties, or their purpose. If you could substitute them for a dictionary, you'd have little trouble implementing this.
// assuming this returns a List<string> containing state names such as "Vermont"
var states = GetStatesHavingCustomers();
var stateRegions = new Dictionary<string, string>();
stateRegions["Vermont"] = "NorthEast";
stateRegions["Massacheusetts"] = "NorthEast";
//etc..
// Returns an IEnumerable<string>, with distinct entries
var dbRegions = (from entry in stateRegions where states.Contains( entry.Key ) select entry.Value).Distinct();
精彩评论