通过子实体的属性过滤聚合根实体和子实体
希望有人可以帮忙解决这个问题!
我将根据标准的Order-->OrderLine-->Product而不是实际情况来举一个例子,以便于解释!
基本上,我想运行一个查询,返回包含电视的订单行的所有订单。足够简单:
IEnumerable<Order> orders;
using (var context = new DataContext())
{
var source =
context.Orders.Include("OrderLines").Include(
"OrderLines.Product");
orders= source.Where(o => o.OrderLines.Where(ol => ol.Product.Name == "TV")).ToList();
}
return orders;
这在我获得正确的 Order 实体集合的意义上是有效的,但是当我使用查看每个 Order 的 OrderLines 集合时,它包含所有 OrderLines 而不仅仅是包含 TV 的 OrderLines。
希望这是有道理的。
预先感谢您的任何帮助。
Hope that someone out there can help with this!
I'll give an example based on the standard Order-->OrderLine-->Product rather than the actual situation to make it easier to explain!
Basically, I want to run a query that returns all orders for which there is an order line containing a TV. Simple enough:
IEnumerable<Order> orders;
using (var context = new DataContext())
{
var source =
context.Orders.Include("OrderLines").Include(
"OrderLines.Product");
orders= source.Where(o => o.OrderLines.Where(ol => ol.Product.Name == "TV")).ToList();
}
return orders;
This works in the sense that I get the correct collection of Order entities, but when I use look at each Order's collection of OrderLines it contains all OrderLines not just those containing at TV.
Hope that makes sense.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我确实有道理,因为查询满足您最初的条件“返回包含电视的订单行的所有订单”,每个订单当然都会包含所有订单行。该过滤器仅用于选择订单,而不是订单行。
要仅从订单中检索包含 TV 的 OrderLines,您需要再次使用过滤器,因此:
I does make sense in that the query is fulfilling your original criteria "to return all orders for which there is an order line containing a TV", each order will of course have all the orderlines. The filter is only being used to select the Orders, not the OrderLines.
To retrieve just the OrderLines containing TV from an Order you'd use the filter again, thus:
要点是了解您是否需要在过滤行中保留(或不需要)对订单标题的引用。
即,您是否想要所有电视订单的列表,更准确地说,只是他们的电视线路?或者你想要所有的电视线路不管他们的订单标题吗?
您似乎更喜欢第一个选项。
那么最好的解决方案肯定是
获取相关订单,然后,对于 relatedOrders 中的每个订单:
仅考虑电视线路。
其他技术会导致信息丢失或迫使您构建一个与初始订单集合类似的新订单集合,但在标题和行上进行双重过滤,就优雅和性能而言,这似乎相当糟糕。
The main point is to know if you need to keep (or not) a reference to the order header in the filtered lines.
I.e. do you want the list of all the orders with a TV, and more precisely only their TV lines ? or do you want all the TV lines nevermind their order header ?
You seem to prefer the first option.
Then the best solution would certainly be
to get the relevant orders, and then, for each order in relevantOrders :
to consider only the TV lines.
Other techniques would result in a loss of information or force you to build a new orders collection similar to the initial one but double-filtered on the headers and on the lines, which seems fairly bad as far as elegance and performance is concerned.