Count in LINQ using Lambda Expressions

Posted by: Jason N. Gaylords Blog, on 01 May 2008 | View original | Bookmarked: 0 time(s)

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!

Category: SQL | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1 | Hits: 0

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD