Showing posts with label Linq. Show all posts
Showing posts with label Linq. Show all posts

Wednesday, 3 June 2009

Linq Performance

Something to think carefully when using Linq is performance of certain extensions.

If you have a large in memory collection that you wish to find an element in and you know that there is only one, your first instinct maybe to use the Single operator. Now, unless you explicitly want to check that there is only one item that matches your predicate, then you will incur the overhead of always searching the entire list. But, if you know that there is only one item that should match your predicate and ensuring this isn't called for, then First should be the operator of choice as this will stop searching as soon as it finds the item that matches the predicate.

Now, whilst this might seem like a small saving, it is saving that doesn't take much thought to implement.

Submit this story to DotNetKicks Shout it