从具有多对多关系的实体中进行选择 [英] Selecting from Entity with Many-to-Many Relationship

查看:79
本文介绍了从具有多对多关系的实体中进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个数据库结构,例如此链接中的结构: http://www.tomjewett .com / dbdesign / dbdesign.php?page = manymany.php ,其中存在多对多关系(例如订单和产品),如何使用EF创建将返回所有订单的查询包含给定产品列表中任何一个的产品?
例如,返回所有包含苹果,橙子和胡萝卜的订单,这些订单可以存储在List< string>中。变量。如果我只需要单个产品的所有订单,那就很容易了:所有订单都在这里(i => i.Products。)任何(p => p.prodName ==" Apple"));



如何修改它以支持存在任意数量产品的所有订单?

谢谢。

解决方案

Stanley-

我认为你所描述的问题在这个主题中得到解决: http://social.msdn.microsoft.com /论坛/ EN-US / adodotnetentityframework /线程/ 095745fe-dcf0-4142-b684-b7e4a1ab59f0 / 。我认为科林提供了一种实现你想要的方法。而且,正如他指出的那样,我们正在努力使这个场景在下一个EF版本中更容易。

希望有所帮助。

- 布莱恩


Given a database structure such as the one in this link: http://www.tomjewett.com/dbdesign/dbdesign.php?page=manymany.php, where there is a Many-to-Many relationship (e.g. Order and Product), how can I use EF to create a query that would return all Orders that contain any one of a given list of Products?

For example, return all orders that have Apples, Oranges, and Carrots, which may be stored in a List<string> variable.  If I only needed all orders with a single product, that would be easy:


allOrders.Where(i => i.Products.Any(p => p.prodName == "Apple"));


How can this be modified to support all orders where any number of products exist?

Thank you.

解决方案

Stanley-

I think the issue you are describing is addressed in this thread: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/095745fe-dcf0-4142-b684-b7e4a1ab59f0/ . I think Colin provides a way to achieve what you want. And, as he points out, we are working to make this scenario much easier in the next EF release.

Hope that helps.

-Brian


这篇关于从具有多对多关系的实体中进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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