选择内部选择(SQL到lambda linq表达式)

时间:2015-02-02 13:41:29

标签: c# linq entity-framework asp.net-mvc-4 lambda

Select Label,
(SELECT COUNT(*) from [CourtSessions] cs where cs.iDCity = Cit.ID) as courts, 
(Select COUNT(*) from [Cases] c inner join [CourtSessions] cs ON c.ID = cs.iDCase where cs.iDCity = Cit.ID) as csnatures 
FROM Cities Cit
Group by Label, id

我试过了,但它没有用

var data = db.Cities
      .GroupBy(a => a.label)
      .Select(g => new
      {
          city = g.Key,
          sessions = db.CourtSessions.Include(p => p.CityTB).Count(o => o.CityTB.label == g.Key),
          cases = db.Cases.Join(db.CourtSessions, u => u.ID, ui => ui.iDCase, (u, ui) => new { u, ui }).Count(m => m.ui.CityTB.label == g.Key)

      });

CityTB是外键的地方

案件(身份证......)
城市(ID,标签)
CourtSession(ID,iDCase,iDCity ...... CasesTB,CityTB)

我收到此异常

  

base {System.Exception} = {" LINQ to Entities无法识别方法' System.Linq.IQueryable 1[LawbookMVC.Models.CourtSession] Include[CourtSession,City](System.Linq.IQueryable 1 [LawbookMVC.Models.CourtSession],System.Linq。 Expressions.Expression 1[System.Func 2 [LawbookMVC.Mod ...

感谢。

1 个答案:

答案 0 :(得分:0)

我解决了,谢谢大家

var dat = db.Cities
          .GroupBy(a => new { a.label, a.ID})
          .Select(g => new
          {
              city = g.Key.label,
              sessions = db.CourtSessions.Count(o => o.iDCity == g.Key.ID),//,
              cases = db.Cases.Join(db.CourtSessions, u => u.ID, ui => ui.iDCase, (u, ui) => new { u, ui }).Count(m => m.ui.CityTB.label == g.Key.label)

          });