Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架加载过滤器_Entity Framework_Eager Loading - Fatal编程技术网

Entity framework 实体框架加载过滤器

Entity framework 实体框架加载过滤器,entity-framework,eager-loading,Entity Framework,Eager Loading,我有一个简单的查询,我想这样做: 1) 产品有子产品它们有价格层 2) 我想获得所有产品,这些产品的类别,其ID为1且显示=true。 3) 然后,我想包括所有显示为true的子产品。 4) 然后包括IsActive=true的PriceTiers 据我所知,EF不支持使用过滤器进行即时加载,因此以下操作不起作用: ProductRepository.Query.IncludeCollection(Function(x) x.ChildProducts.Where(Function(y) y.D

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

1)
产品
子产品
它们有
价格层

2) 我想获得所有
产品
,这些产品的
类别
,其
ID
为1且
显示
=true。
3) 然后,我想包括所有显示为true的
子产品。
4) 然后包括
IsActive
=true的
PriceTiers

据我所知,EF不支持使用过滤器进行即时加载,因此以下操作不起作用:

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

有什么建议吗?

从下到上开始,意思是对
PriceTier
对象及其父对象应用过滤器,并包括其父对象(对不起,希望您能理解这一点):

(注意:C中的
priceTier=>
与VB.NET中的
函数(priceTier)
相同)

执行查询时,
MergeOption
理想情况下应设置为其他而不是
NoTracking
。否则,EF将不会确保在查询结果集中多次出现的对象只具体化一次,例如
产品
子产品

不需要的结果: PriceTier 1和PriceTier 2具有相同的父级,但父级已实现多次-每个PriceTier一次

  • 产品1
    • 儿童产品1
      • 价格一级
  • 产品1
    • 儿童产品1
      • 价格等级2
理想结果:
MergeOption
设置为除
NoTracking
以外的任何选项以获得以下结果:

  • 产品1
    • 儿童产品1
      • 价格一级
      • 价格等级2

这里有一个解决方案,通过使用左连接而不是快速加载,并包括不存在子行的筛选器的父行,使您的“行”与您的请求相匹配

var query = from product in Products
                join child_ in ChildProducts on product equals child_.Product into child_join
                from child in child_join.DefaultIfEmpty()
                join tier_ in PriceTiers on child equals tier_.ChildProduct into tier_join
                from tier in tier_join.DefaultIfEmpty()
                where product.Display && product.Category.ID == 1
                where child == null || child.Display
                where tier == null || tier.IsActive
                select new {product, child, tier};

为什么你要为同一个问题提出多个问题?此外,你为什么要一次又一次地这样做呢?从下至上开始会产生一个副作用,那就是如果父母存在,并且没有与过滤器匹配的孩子,那么这些父母就不会出现在结果集中。5年后,你可以使用左连接来实现你的过滤器治理这仍然是EF的问题,你的答案仍然是最好的解决方案。谢谢
var query = from product in Products
                join child_ in ChildProducts on product equals child_.Product into child_join
                from child in child_join.DefaultIfEmpty()
                join tier_ in PriceTiers on child equals tier_.ChildProduct into tier_join
                from tier in tier_join.DefaultIfEmpty()
                where product.Display && product.Category.ID == 1
                where child == null || child.Display
                where tier == null || tier.IsActive
                select new {product, child, tier};