Have the following:
class Hal
{
public int zip { get; set; }
public string Phone { get; set; }
...
}
List<Hal> data;
Dictionary<int, List<int>> zipList;
zipList
– key is “zip”
– value is a collection of related “zip”
Want to select all “Hal” objects with “zip” from ziplist “key” AND all related “zip” from ziplist “value”.
How on earth do I do this in c# linq?
So you mean every
Halindatawhere thezipis either in the key or the value ofzipList? I’d probably use:To explain:
zipList.Values.SelectMany(x => x)will just create a flattened view of all the valuesHashSet<int>from that for simplicity and efficiency of checking in theWhereclause in a moment; alternatively a join would do this for us, but it feels simpler not to join when we’re really only interested in one sideWhereclause just filters the list ofHalobjects to those with a requiredzip