你能帮我把这个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,但没有任何东西适合这种情况。有可能吗?
由于
答案 0 :(得分:3)
我想你想要这样的东西
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(),
};