Search code examples
.netlinqsorteddictionary

Is LINQ Where extension method optimized for SortedDictionary?


Folks,

If I call the LINQ Where extension method, does it take advantage of the sorting in SortedDictionary or does it traverse each KVP and make the comparison? Is there an advantage to using SortedDictionary for the search-by-criteria scenario?

Thanks!


Solution

  • No, whilst LINQ methods attempt a few basic casts (ie to ICollection when a length estimate is needed), they cannot begin to cast the IEnumerable to every .net collection out there to see if they can make use of that collection's properties.

    However as you know the properties of your SortedDictionary source, could you use TakeWhile or SkipWhile instead of Where?