Vote UpVote

In LINQ, Don’t Use Count() When You Mean Any()

Craig Stuntz's Weblog – If you have a list, array, or query in a C#/LINQ application and need to check and see if the list is empty, the correct way to do this is to use the Any() extension method: if (q.Any()) { Similarly, you can check to see if any elements in the list meet a certain condition: if (q.Any(i => i.IsSpecial)) { If the query provider is something like LINQ to Entities, this will be translated into fairly efficient SQL using EXISTS. For some reason, I see a lot of people write this code using the Count() extension method instead (maybe they don’t know about Any()?), like this: if ...
Favorite? Off-Topic? Craig Stuntz @ 2010-04-21 23:01


Visits: 304
Votes: 0
Favorites: 0
Off-Topic: 1

Visits by Source

User Actions

Users who voted for this posting

Contact us to advertise on DelphiFeeds.com

Community Links

Torry Firebird News


Please login or register to use this functionality.
(click on this box to dismiss)