Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()?

like image 281
Anup Avatar asked Dec 24 '13 04:12

Anup


People also ask

How to get Count in LINQ c#?

You should be able to do the count on the purch variable: purch. Count(); e.g.

How to use Count() in c#?

In its simplest form (without any parameters), the Count() method returns an int indicating the number of elements in the source sequence. IEnumerable<string> strings = new List<string> { "first", "then", "and then", "finally" }; // Will return 4 int result = strings. Count();

Is in Linq C#?

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support.


3 Answers

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"); 
like image 79
Alexander Galkin Avatar answered Oct 21 '22 10:10

Alexander Galkin


var recordCount = ctx.Cases.Count(a => a.Role == "admin" && a.Userid="userid");
like image 35
MaTya Avatar answered Oct 21 '22 09:10

MaTya


First give Where and then count.

ctx.Cases.Where(c => your condition).Count();
like image 39
AbhinavRanjan Avatar answered Oct 21 '22 08:10

AbhinavRanjan