实体框架加载过滤器 [英] Entity Framework Eager Loading Filter

查看:137
本文介绍了实体框架加载过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的查询,我想这样做:



1)产品 ChildProducts 其中有 PriceTiers

2)我想获得所有的产品具有类别 ID 1和显示 = true。

3)我想要包含所有 ChildProducts ,其中包含显示 = true。

4)然后包含 PriceTiers ,其中包含 IsActive = true。



从我读过的内容中,EF不支持使用过滤器加载,因此以下内容将无法正常工作:

  ProductRepository.Query.IncludeCollection(Function(x)x.ChildProducts.Where(Function(y)y.Display).Select(Function(z)z.PriceTiers)其中(Function(q)q.IsActive)))。其中(Function(x)x.Categories.Any(Function(y)y.ID = ID)))
pre>

任何建议?

解决方案

从意思是在 PriceTier 对象及其父母之间应用过滤器,并包含其父级(C#抱歉,但希望您得到这一点):

  repository.PriceTiers 
.Include(ChildProduct.Product)// eager load parents
.Where(priceTier => ;
priceTier.IsActive&&
priceTier.ChildProduct.Display&&
priceTier.ChildProduct.Product.ID == 1&&
priceTier.ChildProduct.Product.Display)
.AsEnumerable()//执行SQL语句
.Select(priceTier =>
priceTier.ChildProduct.Product)//返回产品而不是价格层

(注: priceTier => 与VB.NET中的 Function(priceTier)相同)



MergeOption 在执行查询时应理想地设置为其他 NoTracking 。否则,EF不会确保查询结果集中多次出现的对象只会实现一次,例如 Product ChildProduct



不需要的结果
PriceTier 1和2有相同的父母,但父母有已经实现了多次 - 每个PriceTier一次。




  • 产品1

    • ChildProduct 1

      • PriceTier 1



  • 产品1

    • ChildProduct 1

      • PriceTier 2





理想的结果:
设置 MergeOption NoTracking 之外的任何东西,以获得这些结果:




  • 产品1

    • ChildProduct 1

      • PriceTier 1

      • PriceTier 2




I have a simple query I want to do like this:

1) Products have ChildProducts which have PriceTiers
2) I want to get all the Products that have a Category with a ID of 1 and Display = true.
3) I want to then include all the ChildProducts that have Display = true.
4) And then include the PriceTiers that have IsActive = true.

From what I have read, EF does not support Eager Loading with filters, so the following will not work:

ProductRepository.Query.IncludeCollection(Function(x) x.ChildProducts.Where(Function(y) y.Display).Select(Function(z) z.PriceTiers.Where(Function(q) q.IsActive))).Where(Function(x) x.Categories.Any(Function(y) y.ID = ID)))

Any suggestions?

解决方案

Start from the bottom up, meaning, apply filter on the PriceTier object and its parents, and include its parents (C# sorry, but hopefully you get the point):

repository.PriceTiers
  .Include("ChildProduct.Product") // eager load parents
  .Where(priceTier => 
    priceTier.IsActive &&
    priceTier.ChildProduct.Display &&
    priceTier.ChildProduct.Product.ID == 1 &&
    priceTier.ChildProduct.Product.Display)
  .AsEnumerable() // execute SQL statement
  .Select(priceTier => 
    priceTier.ChildProduct.Product) // return products rather than price tiers

(Note: priceTier => in C# is the same as Function(priceTier) in VB.NET)

MergeOption should ideally be set to something other than NoTracking when executing the query. Otherwise, EF will not ensure that an object that appears multiple times in the result set of the query is only materialized once, such as a Product or ChildProduct:

Unwanted results: PriceTier 1 and 2 have the same parents, but the parents have been materialized multiple times - once for each PriceTier.

  • Product 1
    • ChildProduct 1
      • PriceTier 1
  • Product 1
    • ChildProduct 1
      • PriceTier 2

Ideal results: Set MergeOption to anything other than NoTracking to get these results:

  • Product 1
    • ChildProduct 1
      • PriceTier 1
      • PriceTier 2

这篇关于实体框架加载过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆