简体   繁体   English

在LINQ的SELECT到Entities查询中添加第二个查询会出现无法识别的方法错误

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

I have built a LINQ query for to fill one of my ViewModel with data but inside the .SELECT() method I tried to add another query to populate one of the VM's properties. 我建立了一个LINQ查询,用于用数据填充我的ViewModel之一,但是在.SELECT()方法内部,我试图添加另一个查询以填充VM的一个属性。 When I ran the page I got the error: 当我运行页面时,出现错误:

LINQ to Entities does not recognize the method 'System.Linq.IQueryable 1[FFInfo.DAL.Location] Include[Location](System.Linq.IQueryable 1[FFInfo.DAL.Location], System.String)' method, and this method cannot be translated into a store expression. LINQ to Entities无法识别方法'System.Linq.IQueryable 1[FFInfo.DAL.Location] Include[Location](System.Linq.IQueryable 1 [FFInfo.DAL.Location],System.String)'方法和此方法方法不能转换为商店表达式。

The reason I am using the second query is because LocationTransitionPoints does link directly to Locations (via ToLoation and FromLocation ) but it does not link directly to Section and I don't know how to work my way up to it (LocationTranitionPoints -> Locations -> Section) 我使用第二个查询的原因是因为LocationTransitionPoints确实直接链接到Locations (通过ToLoationFromLocation ),但是它没有直接链接到Section而且我不知道该如何工作(LocationTranitionPoints-> Locations- >部分)

I have a felling I am just missing some stupid little thing but can't place my finger on it, any suggestions please? 我砍伐树木,只是想念一些愚蠢的小东西,但不能将手指放在上面,请问有什么建议吗?

        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");
        }
    }

Could you just do something like this? 你能做这样的事情吗?

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();

Also, I cut the .Include() parts as they're not really necessary since you're projecting into the view model. 另外,由于要投影到视图模型中,因此.Include()部分,因为它们并不是真正必需的。

If you haven't disabled lazy loading you should try with the solution that was proposed by @Peter. 如果您尚未禁用延迟加载 ,则应尝试使用@Peter提出的解决方案。 Otherwise, you can also load the Section nav property as part of your query, including it in the path that you pass as parameter in the Include method: 否则,您还可以将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();
           //...
}

You can load deeper levels following this pattern: 您可以按照以下模式加载更深的级别:

Include("FirstLevelNavProp.SecondLevelNavProp...")

But a better idea is use this another Include extension method which is strongly typed: 但是更好的主意是使用另一个强类型的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();
            //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM