Sql 计数内的LINQ Case语句
你能帮我把这个sql翻译成Linq吗Sql 计数内的LINQ Case语句,sql,linq,count,case,Sql,Linq,Count,Case,你能帮我把这个sql翻译成Linq吗 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 我在互联网上搜索了一些类似的sql,但没有找到适合这种情况的sql。
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
我在互联网上搜索了一些类似的sql,但没有找到适合这种情况的sql。可能吗
谢谢我想你想要这样的东西
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(),
}
编辑-如果希望在一个查询中完成所有操作,可以执行以下操作:
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(),
};
你说得绝对正确,我在发布后就想到使用扩展方法会更容易。谢谢只要它允许,我就把它标为答案