在LINQ to Entities查询的SELECT中添加第二个查询会产生Not Recognized Method错误

本文关键字:查询 Not 第二个 Method Recognized 错误 添加 to LINQ Entities SELECT | 更新日期: 2023-09-27 18:12:26

我已经构建了一个LINQ查询来填充我的ViewModel中的一个数据,但在.SELECT()方法中,我试图添加另一个查询来填充虚拟机的属性之一。当我运行页面时,我得到了错误:

LINQ to Entities不能识别方法System.Linq.IQueryable 1[FFInfo.DAL.Location] Include[Location](System.Linq.IQueryable 1[FFInfo.DAL. info]。[Location], System.String)'方法,并且该方法不能转换为存储表达式。

我使用第二个查询的原因是因为LocationTransitionPoints直接链接到Locations(通过ToLoationFromLocation),但它不直接链接到Section,我不知道如何工作到它(LocationTranitionPoints -> Locations -> Section)

我有一种感觉,我只是错过了一些愚蠢的小东西,但不能把我的手指放在它,有什么建议吗?

        public ActionResult TransitionPoints()
    {
        try
        {
            using (var db = new GeographyContext())
            {
                var model = db.LocationTransitionPoints
                              .Include("FromLocation")
                              .Include("ToLocation")
                              .Select(ltp => new TransitionPointsVM()
                              {
                                  FromLocation = ltp.FromLocation.Name,
                                  ID = ltp.ID,
                                  SectionTitle = db.Locations.Where(l => l.ID == ltp.ToLocationID).Include("Section").Select(l => l.Section.Title).First(),
                                  ToLocation = ltp.ToLocation.Name,
                                  TransitionPoint = ltp.TransitionPoint
                              }).ToList();
                return View(model);
            }
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
            return PartialView("LoadError");
        }
    }

在LINQ to Entities查询的SELECT中添加第二个查询会产生Not Recognized Method错误

如果你还没有禁用延迟加载,你应该尝试一下@Peter提出的解决方案。否则,您还可以将Section nav属性作为查询的一部分加载,并将其包含在作为参数传递给Include方法的路径中:

 using (var db = new GeographyContext())
 {
      var model = db.LocationTransitionPoints
                    .Include("FromLocation")
                    .Include("ToLocation.Section")
                    .Select(ltp => new TransitionPointsVM()
                    {
                      FromLocation = ltp.FromLocation.Name,
                      ID = ltp.ID,
                      SectionTitle = ltp.ToLocation.Section.Title, 
                      ToLocation = ltp.ToLocation.Name,
                      TransitionPoint = ltp.TransitionPoint
                    }).ToList();
           //...
}

您可以按照以下模式加载更深的级别:

Include("FirstLevelNavProp.SecondLevelNavProp...")

但是更好的方法是使用另一个强类型的Include扩展方法:

 using (var db = new GeographyContext())
 {
      var model = db.LocationTransitionPoints
                    .Include(ltp=>ltp.FromLocation)
                    .Include(ltp=>ltp.ToLocation.Section)
                    .Select(ltp => new TransitionPointsVM()
                    {
                      FromLocation = ltp.FromLocation.Name,
                      ID = ltp.ID,
                      SectionTitle = ltp.ToLocation.Section.Title, 
                      ToLocation = ltp.ToLocation.Name,
                      TransitionPoint = ltp.TransitionPoint
                    }).ToList();
            //...
}

你就不能这样做吗?

var model = db.LocationTransitionPoints
  .Select(ltp => new TransitionPointsVM()
  {
      FromLocation = ltp.FromLocation.Name,
      ID = ltp.ID,
      // get to Section through ToLocation
      SectionTitle = ltp.ToLocation.Section.Title,
      ToLocation = ltp.ToLocation.Name,
      TransitionPoint = ltp.TransitionPoint
  }).ToList();

此外,我削减了.Include()部分,因为它们不是真正必要的,因为你是投影到视图模型