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: 1428 | Hits: 11

Similar Posts

  • Eschewing Date Types in our Database more
  • Fun with Func<T,TResult> Delegates, Events and Async Operations more
  • Linq: Beware of the 'Access to modified closure' demon more
  • LINQ in Action samples in LINQPad more
  • An Extensive Examination of LINQ: Using the Query Syntax more
  • An Extensive Examination of LINQ: The Ins and Outs of Query Operators more
  • An Extensive Examination of LINQ: Lambda Expressions and Anonymous Types more
  • An Extensive Examination of LINQ: Extension Methods, Implicitly Typed Variables, and Object Initializers more
  • Extension Methods Part 2 Lambda Expressions in Linq more
  • Creating LINQToTwitter library using LinqExtender more

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