I'm refactoring this code and was trying to think of a simple linq expression to populate this Dictionary.
IEnumerable<IHeaderRecord> headers = PopulateHeaders();
var headerLocationLookup = new Dictionary<string, IHeaderRecord>();
foreach (var header in headers)
{
//destination locations can repeat, if they do, dictionary should only contain the first header associated with a particular location
if (!headerLocationLookup.ContainsKey(header.DestinationLocation))
{
headerLocationLookup[header.DestinationLocation] = header;
}
}
I could only come up with implementing a custom IEqualityComparer and using that in an expression such as this...
headers.Distinct(new CustomComparer()).ToDictionary();
Is there a way to do it all inline without the custom IEqualityComparer? Thanks in advance.