简体   繁体   English

EF如何使用.include()和使用存储库模式查询更多实体

[英]EF How to query more entities with .include() and using repository pattern

I got the following sql statement that I want to implement with entity framework with linq (lambda expression). 我得到了以下要使用linq(lambda表达式)实体框架实现的sql语句。 Here is the SQL: 这是SQL:

select *
from tbl_ExampleStoneCatalog 
join tbl_ExampleStoneCategory 
on tbl_ExampleStoneCatalog.fk_ESC = tbl_ExampleStoneCategory.pk_ESC 
join tbl_ExampleStones
on tbl_ExampleStoneCatalog.fk_ES = tbl_ExampleStones.pk_ES
join tbl_ExampleReviewStoneCatalog 
on tbl_ExampleStones.pk_ES = tbl_ExampleReviewStoneCatalog.fk_ES
where .fk_StoneCategory = '%someParameter%'

I tried to use the .include() which brings me to this: 我试图使用.include()来使我明白这一点:

var res = (await this._exampleStoneCatalog.Query()
          .include(esc => esc.ExampleStoneCategory)
          .include(es => es.ExampleStones)
          .include(es => es.ExampleStones.ExampleReviewStoneCatalog))
          .Where(w => w.ExampleStones.ExampleReviewStoneCatalog.Any(
           a => a.StoneCategoryID.Equals(%someParameter%)));

Unfortunately the code stated above won't deliver me the desired result. 不幸的是,上述代码无法为我提供理想的结果。 Furthermore there is a nested Where condition in it => ExampleStones.ExampleReviewStoneCatalog.StoneCategoryID . 此外,其中还有一个嵌套的Where条件=> ExampleStones.ExampleReviewStoneCatalog.StoneCategoryID From what I understand after some research is, that this is not solvable easily with .include(). 据我研究后了解到,使用.include()很难解决。

Is there other ways to filter in nested queries using the lambda expression? 还有其他方法可以使用lambda表达式过滤嵌套查询吗?

If seems like a many-to-many relationship. 如果似乎是多对多的关系。 I always find it easiest to begin with the connecting table here. 我总是觉得从这里的连接表开始是最容易的。

var res = _tbl_B.Repository.Where(b => b.c.Value == "whatever" && b.a.Value == "whatever").Select(b => b.a);

I have found a work around for this problem. 我已经找到解决此问题的方法。 The main challenge here is to filter in a nested SQL query. 这里的主要挑战是过滤嵌套的SQL查询。 I could not find a solution with .include(). 我无法使用.include()找到解决方案。 Especially my current work environment in which we are useing repository pattern wouldn't allow me to filter within includes like: 尤其是我当前使用存储库模式的工作环境不允许我在以下内容中进行过滤:

var res = await this._exampleStoneCatalog.Query().include(x => x.ExampleStones.ExampleReviewStoneCatalog.Where(w => w.StoneCategoryID.Equals(%SomeParameter%))).SelectAsync();

Hence I come to the following solution with using linq to sql. 因此,我使用linq到sql来解决以下问题。

My solution: 我的解决方案:

 var exampleStoneCatalogEnum = await this._exampleStoneCatalog.Query().SelectAsync();
 var exampleStoneCategoryEnum = await this._exampleStoneCategoryRepository.Query().SelectAsync();
 var exampleStonesEnum = await this.exampleStonesRepository.Query().SelectAsync();
 var exampleReviewStoneCatalogEnum = await this.exampleReviewStoneCatalogRepository.Query().SelectAsync();

 var result = from exampleStoneCatalog in exampleStoneCatalogEnum
              join exampleStoneCategory in exampleStoneCategoryEnum on exampleStoneCatalog.Id equals exampleStoneCategory.Id
              join exampleStones in exampleStonesEnum on exampleStoneCatalog.Id equals exampleStones.Id
              join exampleReviewStoneCatalog in exampleReviewStoneCatalogEnum on exampleStones.Id equals exampleReviewStoneCatalog.Id
              where exampleReviewStoneCatalog.StoneCategoryID.Equals(revCategory)
              select exampleStoneCatalog;
 return result;

as you can see I first get the required data of each table and join them in my result including the where condition in the end. 如您所见,我首先获取每个表的必需数据,然后将它们加入到我的结果中,包括最后的where条件。 This returns the desired result. 这将返回所需的结果。

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

相关问题 Linq to Entities with Repository Pattern 和 EF - Linq to Entities with Repository Pattern and EF 工作单元/存储库模式以及使用EF更新实体 - Unit of Work/Repository pattern and updating entities using EF EF 包括其他实体(通用存储库模式) - EF Including Other Entities (Generic Repository pattern) 如何在 Ef Core 的通用存储库模式中添加包含? - How to add include in Generic repository pattern in Ef Core? 首先使用具有EF代码的存储库模式时的Linq子查询 - Linq sub query when using a repository pattern with EF code first 如何在EF中使用存储库模式使用存储过程和复杂类型? - How to use Store Procedure and Complex types using Repository Pattern in EF? 如何使用存储库模式将 EF 对象转换为 WCF 模型 - How to transform EF Objects to WCF Models using Repository pattern 如何使用存储库模式访问EF中的导航属性 - How to access navigation properties in EF using repository pattern EF 4:如何使用具有存储库模式的MVC正确更新DbContext中的对象 - EF 4: How to properly update object in DbContext using MVC with repository pattern EF Core 包括其他实体(通用存储库模式) - EF Core Including Other Entities (Generic Repository pattern)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM