【问题标题】:Re-using result of an EF JOIN query (lambda) to build another query重新使用 EF JOIN 查询 (lambda) 的结果来构建另一个查询
【发布时间】:2019-06-24 22:39:34
【问题描述】:

我想知道是否可以使用来自Entity Framework 连接查询的结果。

请忽略连接查询是否有意义。我只是想解释我的问题,而不是提出正确的查询。为了清楚起见,我将其简化了很多:

var firstJoinQuery = (from company in this.TimesheetsContext.companies
                      join country in this.TimesheetsContext.Countries
                      on company.CountryId equals country.Id
                      where (country == 'USA')
                      select new { CountryId = country.Id }).Distinct();

var secondJoinQuery = (from country in this.TimesheetsContext.Countries
                      join firstJoinQuery
                      on country.CountryId equals firstJoinQuery.CountryId
                      select new { Country = country }).Distinct();

我发誓我以前做过,但是我无法得到一个联接的结果,以便在第二个或第三个中使用。

我想这样做的原因是为了让事情更容易阅读,因为在使用 EF 和复杂查询时它并不总是很明显。

我目前的解决方法转向实际的存储过程,因为它肯定更容易阅读,但如果可能,我想先在 EF 中试一试。

谢谢。

【问题讨论】:

    标签: c# entity-framework join


    【解决方案1】:

    您可以将两个查询与Union 结合起来。

    【讨论】:

      【解决方案2】:

      我认为你可以做到,但你需要在查询中使用.ToList(),因为Distinct() 执行就像延迟一样。在这里,您尝试像查询字符串一样连接查询,而不是结果查询。

      var firstJoinQuery = (from company in this.TimesheetsContext.companies
                            join country in this.TimesheetsContext.Countries
                            on company.CountryId equals country.Id
                            where (country == 'USA')
                            select new { CountryId = country.Id }).Distinct().ToList;
      
      var secondJoinQuery = (from country in this.TimesheetsContext.Countries
                            join firstJoinQuery
                            on country.CountryId equals firstJoinQuery.CountryId
                            select new { Country = country }).Distinct().ToList();
         var thirdJoinQuery .....
      

      【讨论】:

        【解决方案3】:

        我最终通过更多研究弄明白了。

        我找到了以下文章:Combining LINQ Queries (or, When to Call ToList),这很有帮助。

        为了解决我的问题,我必须返回完整的对象而不仅仅是一个属性,删除 .Distinct() 和 .ToList() 所以我改变了这个:

        var firstJoinQuery = (from company in this.TimesheetsContext.companies
                          join country in this.TimesheetsContext.Countries
                          on company.CountryId equals country.Id
                          where (country == 'USA')
                          select new { CountryId = country.Id }).Distinct().ToList;
        

        var firstJoinQuery = (from company in this.TimesheetsContext.companies
                          join country in this.TimesheetsContext.Countries
                          on company.CountryId equals country.Id
                          where (country == 'USA')
                          select company;
        

        我还没有玩完这个,也没有完成对完整链接的查询,但如果我找到了,我会更新这个答案。

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-21
          • 2016-04-06
          相关资源
          最近更新 更多