在C#中将SQL子查询转换为LINQ

时间:2017-03-14 05:52:50

标签: sql linq subquery

我需要将以下SQL行转换为LINQ查询:

select  
(  select count(*)
   from vpassSlipApp a
   where a.statusID=0 and a.apprvEIC='xxxxx'
) as pass_slip_app
,
(  select count(*)
   from vPtlosApp a
   where a.Tag=3 and a.approveEIC='xxxxx'
) as ptlos_app

我想这样做:

var list = ( new {
      pass_slip_app = from r in db.vpassSlipApps
                      where r.statusID == 0
                      where r.apprvEIC == approvingEIC
                      select r.EIC.Count(),
      ptlos_app = from g in db.vPtlosApps
                      where g.Tag == 3
                      where g.approveEIC == approvingEIC
                      select g.controlNo.Count()
                  }
);

1 个答案:

答案 0 :(得分:0)

你真的需要一份清单吗?因为sql查询只返回1条记录。 这是将该记录作为单个对象返回的查询。

var x = new { 
            pass_slip_app = vpassSlipApp.Count(a=>a.statusID=0 && a.apprvEIC='xxxxx'), 
            ptlos_app = vPtlosApp.Count(a=>a.Tag=0 && a.approveEIC='xxxxx')
        }