tags:

views:

40

answers:

3

Hi,

I want to return a count of new users since a specific date.

Users table has: UserID, username, dateJoined.

SELECT COUNT(USERID)
FROM Users
where dateJoined > @date

How would this look in linq-to-sql?

Can you use the keyword COUNT?

+1  A: 

I am assuming you have a IEnumberable list called users and some variable called somedate.

int count = users
   .Where<User>(i => i.dateJoined > someDate)
   .Count<User>();

This will work against any IEnumberable list not just Linq to Sql

David Basarab
It should be noted, though, that this is just calling an extension method. It isn't actually LINQ (in the sense that LINQ is a language syntax for querying; you're just using the extension methods that LINQ uses). Nothing wrong with that, just worth noting.
Adam Robinson
+1  A: 
(from u in context.Users where u.dateJoined > date select u).Count()
Adam Robinson
+2  A: 

You can go two routes:

var count = (from u in context.Users where u.datJoined > date select u).Count();

or

var count = context.Users.Where( x => x.datJoined > date).Count();

Both are equivalent, it really boils down to a matter of personal preference.

Joseph
I think the first one doesn't compile because it requires a select, but I'd recommend the second one.
Thomas Danecker
@Thomas thanks for the catch, I modified to include the select.
Joseph