Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 计数内的LINQ Case语句_Sql_Linq_Count_Case - Fatal编程技术网

Sql 计数内的LINQ Case语句

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。

你能帮我把这个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。可能吗


谢谢

我想你想要这样的东西

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(),
            };

你说得绝对正确,我在发布后就想到使用扩展方法会更容易。谢谢只要它允许,我就把它标为答案