Anup
Anup

Reputation: 9738

Linq Count with Condition

I want to provide 2 condition's in the COUNT clause for checking ROLE & USERID.

Here is my code :-

var recordCount = ctx.Cases.Count();

How to give Where condition in Count()?

Upvotes: 22

Views: 35613

Answers (3)

MaTya
MaTya

Reputation: 782

var recordCount = ctx.Cases.Count(a => a.Role == "admin" && a.Userid="userid");

Upvotes: 6

Alexander Galkin
Alexander Galkin

Reputation: 12524

Just add a predicate to your Count() expression (and don't forget to include System.Linq):

var recordCount = ctx.Cases.Count(a => a.Role == "admin");

Upvotes: 41

AbhinavRanjan
AbhinavRanjan

Reputation: 1646

First give Where and then count.

ctx.Cases.Where(c => your condition).Count();

Upvotes: 4

Related Questions