Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a count with linq-to-sql

Tags:

linq-to-sql

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?

like image 724
mrblah Avatar asked Aug 10 '09 14:08

mrblah


People also ask

How to count in LINQ query?

In LINQ, you can count the total number of elements present in the given sequence by using the Count Method. This method returns the total number of elements present in the given sequence.

What method returns the number of items in LINQ query result?

The result of an executed LINQ query has a method Count() , which returns the number of elements it contains.

How does LINQ to SQL work?

In LINQ to SQL, the data model of a relational database is mapped to an object model expressed in the programming language of the developer. When the application runs, LINQ to SQL translates into SQL the language-integrated queries in the object model and sends them to the database for execution.

What is LinqToDB?

LINQ to DB is a data access technology that provides a run-time infrastructure for managing relational data as objects. linq2db.PostgreSQL. by: it ili LinqToDB. last updated a month ago. Latest version: 4.3.0.


1 Answers

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.

like image 54
Joseph Avatar answered Oct 22 '22 19:10

Joseph