I have a large list of objects where I sometimes need to iterate over the entire list and sometimes I only want to look at objects where .Property="somevalue".
Currently I look over my collection .Where(_ => _.Property="value_i_need") which is expensive if I have 100k objects loaded and only a handful are in my target group.
So what I'd like to do is keep the list segmented maybe as a Dictionary<string, List<T>> so that I can quickly refer to only the set of objects I want, but if I do this, how can enumerate all the objects in all the dictionaries using Linq without using more memory to maintain a regular list?
Well it sounds like you want a Lookup:
var lookup = list.ToLookup(x => x.Property);
You can easily iterate over the whole collection by just flattening:
foreach (var entry in lookup.SelectMany(x => x))
{
}
... but this won't be in the initial list order (unless you're very lucky :) If you need to maintain the original list order, you'll need to do a bit more work...
Why not simply make it simple, and store those specific objects in another list ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With