【问题标题】:Select inside select (SQL to lambda linq expression)选择内部选择(SQL 到 lambda linq 表达式)
【发布时间】:2015-02-02 13:41:29
【问题描述】:
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 ...)
城市(ID、标签)
CourtSession(ID、iDCase、iDCity ... CasesTB、CityTB)

我遇到了这个异常

base {System.Exception} = {"LINQ to Entities 无法识别方法 'System.Linq.IQueryable1[LawbookMVC.Models.CourtSession] Include[CourtSession,City](System.Linq.IQueryable1[LawbookMVC.Models.CourtSession], System.Linq.Expressions.Expression1[System.Func2[ LawbookMVC.Mod...

谢谢。

【问题讨论】:

  • 你有什么异常吗?如果是,请在您的问题中指定。
  • 是的,我刚刚包含它
  • 你可能想要select而不是Include

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


【解决方案1】:

我解决了,谢谢大家

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)

          });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多