Entity Framework 4: Eager Loading (Include) with filters using Self Tracking Entities(实体框架 4:使用自跟踪实体的过滤器预加载(包含))
问题描述
我有一个解决方案,我使用 RTM 模板创建了自我跟踪实体.我已经在 2 个项目之间拆分了实体和上下文,以便在我计划通过 WCF 运行客户端/服务器时可以重用类型定义.
I have a solution where I have created self tracking entities using the RTM templates. I have split the entities and context between 2 projects so that I can reuse the type definitions as I plan to run client/server via WCF.
我的一种服务方法需要返回带有ProductSku"子对象的Product"对象图,而这些子对象又具有ProductPrice"子对象.选择标准将基于Product"对象的Name"属性和ProductPriceObject"的FinancialPeriodID"属性.目前,我没有在搜索中包含该名称,但我在恢复图表时遇到了问题.
One of my service methods is required to return a graph of "Product" objects with child objects of "ProductSku" and these in turn have child objects of "ProductPrice". The selection criteria will be on the "Name" property of the "Product" object, and the "FinancialPeriodID" property of the "ProductPriceObject". For now, I am not including the name in the search, but I am having problems bringing back the graph.
如果我只是执行以下查询(注意,此语法取自 LinqPad 而不是实际的应用程序代码)...
If I simply perform the following query (note, this syntax is taken from LinqPad rather than the actual application code)...
from product in Products.Include("Skus.PriceHistory")
select product
...然后我可以检索我需要的项目的完整对象图,当然此时没有过滤器.
... then I am able to retrieve the full object graph for the items that I require, of course at this point there is no filter.
如果相反,我将过滤器介绍如下...
If instead, I introduce the filter as follows...
from product in Products.Include("Skus.PriceHistory")
join sku in ProductSkus on product.ID equals sku.ProductID
join price in ProductPrices on sku.ID equals price.ProductSkuID
where price.FinancialPeriodID == 244
select product
...我期望得到的是Product"对象、子ProductSku"对象(位于Product"的Skus"集合中)及其ProductPrice"对象(它们是在ProductSku"的PriceHistory"集合中) - 但我只取回Product"对象,Skus"集合是空的.
... what I am expecting to get back is the "Product" objects, the child "ProductSku" objects (which are in the "Skus" collection of the "Product") and their "ProductPrice" objects (which are in the "PriceHistory" collection of the "ProductSku") - but I only get back the "Product" objects, the "Skus" collection is empty.
我也尝试将查询编码为 ...
I have also tried coding the query as ...
from product in Products.Include("Skus.PriceHistory")
from sku in product.Skus
from price in sku.PriceHistory
where price.FinancialPeriodID == 244
select product
...但这也没什么区别.
... but this makes no difference either.
显然,我一定做错了什么.任何人都可以阐明那是什么东西,因为我已经在这几个小时里一直在兜圈子!
Clearly, I must be doing something wrong. Can anybody shed any light on what that something is as I have been at this for some hours now going around in circles!
推荐答案
怎么样:
from product in Products.Include("Skus.PriceHistory")
where product.Skus.Any(s => s.PriceHistory.Any(p => p.FinancialPeriodID == 244))
select product
Include
已经执行了所有必要的任务来填充导航属性,因此不需要额外连接 where 条件.更重要的是,任何手动连接或投影都会改变查询的形状,并且不会使用 Include
.
Include
already performs all necessary tasks to fill navigation properties so additional joins for where condition are not needed. What is even more important any manual join or projection will change the shape of the query and Include
will not be used.
还要注意 where 条件仅过滤产品.它不会过滤 Include 加载的数据 - 您将获得至少一个 sku 具有财务周期 id 244 的价格历史记录的所有产品,但这些产品将加载所有 sku 和价格历史记录.EF 当前不支持对包含进行过滤.如果您还需要过滤关系,则必须执行单独的查询来获取它们.
Also beware that where condition filters only products. It will not filter data loaded by Include - you will get all products with at least one sku having price history with financial period id 244 but those products will have all skus and price histories loaded. EF currently does not support filtering on include. If you need filtered relations as well you have to execute separate queries to get them.
这篇关于实体框架 4:使用自跟踪实体的过滤器预加载(包含)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:实体框架 4:使用自跟踪实体的过滤器预加载(包含)
- 在 C# 中异步处理项目队列 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 使用 rss + c# 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01