Return a count with linq-to-sql

2019-06-15 05:05发布

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?

标签: linq-to-sql
3条回答
劫难
2楼-- · 2019-06-15 05:17

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

查看更多
虎瘦雄心在
3楼-- · 2019-06-15 05:27
(from u in context.Users where u.dateJoined > date select u).Count()
查看更多
干净又极端
4楼-- · 2019-06-15 05:33

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.

查看更多
登录 后发表回答