Count in LINQ using Lambda Expressions

I had a table that contained a couple of columns defined in SQL 2005 as tinyint. In SQL, I’d generally write something like:

SELECT COUNT(EmailOptIn)
FROM Members
WHERE EmailOptIn = 1

Of course using LINQ there are lots of different ways to option the count. The simplest way is to use a Lambda Expression. Here’s what I came up with in VB.NET:

db.Members.Count(Function(p) p.EmailOptIn)



Translated in C#, it would be this:



db.Members.Count(p => p.EmailOptIn);


Hope that helps!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>