Enumerable methods .All() and .Where()

Today I made one of my classic mistakes. I didn’t *READ* the MSDN documention on an Extension Method in its entirety. I was discussing some Enumerable extension methods with a friend and looked up the .All() extension method.

Instead of reading all of the code I read how the code was implemented and jumped to some conclusions. I sat ranting about how .All() and .Where() do exactly the samething and how silly that is. Once I actually sat down and read the examples properly I saw the very obvious difference. Here is the difference between the methods though.

.All()

The Enumerable.All() method determines whether all elements of a sequence satisfy a condition according to its MSDN entry. When using this it would be implemented as follows:

users.All(user => user.Name.StartsWith("B"));

This would return a boolean value *ONLY* if all the entries in our users Enumerable had a user.Name starting with the letter “B”.

.Where()

The Enumerable.Where() method filters a sequence of values based on a predicate according to its MSDN entry. When using this it would be implemented as follows:

IEnumerable query = users.Where(user => user.Name.StartsWith("B"));

This would return all the records where the user’s name starts with “B”

I am FailBoy

This is exactly why I have been named FailBoy. So often I only read parts of something and jump to conclusions. The lesson learnt, READ the manual and don’t jump to conclusions!

2 Comments

  1. Roz
    Posted June 21, 2009 at 2:15 PM | Permalink

    Thanks for this – useful bitesized info! From another “FailBoy” type in Germany… proved by this comment ;)

  2. FailBoy
    Posted June 22, 2009 at 7:04 AM | Permalink

    Huge pleasure Roz! Glad to have been of some help :)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*