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!

No Comments