Case LINQ statement in Count

Can you help me translate this sql to Linq, please:

select count(case when t.TypeID = 1 then t.TypeID end) as a, count(case when t.TypeID = 2 then t.TypeID end) as b, count(case when t.TypeID = 3 then t.TypeID end) as c from myTable t 

I searched on the Internet and found some kind of similar sql, but nothing suitable for this scenario. Is it possible?

thanks

+4
source share
1 answer

I think you want something like this

 var query = new { a = (context.MyTable.Where(t => t.TypeID == 1).Count(), b = (context.MyTable.Where(t => t.TypeID == 2).Count(), c = (context.MyTable.Where(t => t.TypeID == 3).Count(), } 

Change If you want all this in one query, you can do this:

 var query = from x in context.MyTable group x by 1 into xg select new { a = xg.Where(t => t.TypeID == 1).Count(), b = xg.Where(t => t.TypeID == 2).Count(), c = xg.Where(t => t.TypeID == 3).Count(), }; 
+3
source

All Articles