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

Adding a second query inside the SELECT of a LINQ to Entities query gives Not Recognized Method error

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

LINQ to Entities does not recognize the method 'System.Linq.IQueryable1[FFInfo.DAL.Location] Include[Location](System.Linq.IQueryable1[FFInfo.DAL.Location], System.String)' method, and this method cannot be translated into a store expression.

我使用第二个查询的原因是因为 LocationTransitionPoints 直接 link 到 Locations(通过 ToLoationFromLocation)但是它确实不是 link 直接到 Section,我不知道如何工作(LocationT运行itionPoints -> 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");
        }
    }

你能做这样的事情吗?

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() 部分,因为它们并不是真正必要的,因为您正在投影到视图模型中。

如果您没有禁用 lazy loading,您应该尝试使用@Peter 提出的解决方案。否则,您还可以加载 Section 导航 属性 作为查询的一部分,将其包含在您作为参数传递给 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();
            //...
}