I currently use the following:
var result = list.Union(otherList).SelectMany(l => l.children).ToList()
foreach(var child in lotsOfChildren){
something = result.Contains(child)
}
I only use result
for querying if the list contains items, using result.Contains(x)
.
This look to be inefficient to me, as, presumably, Contains()
has to enumerate the entire (static) list each time, to check if an item is contained.
I noticed ToDictionary()
and ToLookup()
extension methods, however both these require a key to be defined, which is unnecessary.
I guess what I'm really looking for is a ToHashSet()
sort of thing - would I have to create this manually, as in .ToLookup(c => c.GetHashCode, c => c)
? Or is there a built in method I'm missing? Or, alternatively, is this something the compiler will optimise, and I don't need to care about.
ToList
for efficiency, but it doesn't seem to be required. list and otherList could be 99% equivalent, which is why I was using Union - which is lazy, isn't it?