Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partitioned List<T>?

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?

like image 292
powlette Avatar asked Mar 08 '26 18:03

powlette


2 Answers

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...

like image 58
Jon Skeet Avatar answered Mar 11 '26 06:03

Jon Skeet


Why not simply make it simple, and store those specific objects in another list ?

like image 36
squelos Avatar answered Mar 11 '26 08:03

squelos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!