具有多个左外连接的 Linq to Entity
我试图理解 LINQ to Entity 中的左外连接。 例如,我有以下 3 个表:
Company、CompanyProduct、Product
CompanyProduct 链接到其两个父表 Company 和 Product。
我想返回所有公司记录和关联的 CompanyProduct,无论给定产品的 CompanyProduct 是否存在。 在 Transact SQL 中,我将使用左外连接从 Company 表中获取,如下所示:
SELECT * FROM Company AS C
LEFT OUTER JOIN CompanyProduct AS CP ON C.CompanyID=CP.CompanyID
LEFT OUTER JOIN Product AS P ON CP.ProductID=P.ProductID
WHERE P.ProductID = 14 OR P.ProductID IS NULL
我的数据库有 3 个公司,以及 2 个与 ProductID 14 关联的 CompanyProduct 记录。因此 SQL 查询的结果是预期的 3 行,其中 2 行连接到 CompanyProduct 和 Product 以及 1,其中仅包含 Company 表以及 CompanyProduct 和 Product 表中的空值。
那么如何在 LINQ to Entity 中编写相同类型的联接来获得类似的结果呢?
我尝试了一些不同的方法,但无法获得正确的语法。
谢谢。
I am trying to understand left outer joins in LINQ to Entity. For example I have the following 3 tables:
Company, CompanyProduct, Product
The CompanyProduct is linked to its two parent tables, Company and Product.
I want to return all of the Company records and the associated CompanyProduct whether the CompanyProduct exists or not for a given product. In Transact SQL I would go from the Company table using left outer joins as follows:
SELECT * FROM Company AS C
LEFT OUTER JOIN CompanyProduct AS CP ON C.CompanyID=CP.CompanyID
LEFT OUTER JOIN Product AS P ON CP.ProductID=P.ProductID
WHERE P.ProductID = 14 OR P.ProductID IS NULL
My database has 3 companies, and 2 CompanyProduct records assocaited with the ProductID of 14. So the results from the SQL query are the expected 3 rows, 2 of which are connected to a CompanyProduct and Product and 1 which simply has the Company table and nulls in the CompanyProduct and Product tables.
So how do you write the same kind of join in LINQ to Entity to acheive a similiar result?
I have tried a few different things but can't get the syntax correct.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
解决了!
最终输出:
这是场景
1 - 数据库
2 - 数据
3 - 实体在 VS.NET 2008 中
替代文本 http://i478.photobucket .com/albums/rr148/KyleLanser/companyproduct.png
实体容器名称为“testEntities”(如模型“属性”窗口中所示)
4 - 代码(终于!)
5 - 最终输出
Solved it!
Final Output:
Here is the Scenario
1 - The Database
2 - The Data
3 - The Entity in VS.NET 2008
alt text http://i478.photobucket.com/albums/rr148/KyleLanser/companyproduct.png
The Entity Container Name is 'testEntities' (as seen in model Properties window)
4 - The Code (FINALLY!)
5 - The Final Output
它应该是这样的......
这就是我所做的......
IT should be something like this....
This is how I did....
您将需要使用实体框架来设置从公司到产品的多对多映射。 这将使用 CompanyProduct 表,但无需在实体模型中设置 CompanyProduct 实体。 完成此操作后,查询将非常简单,这取决于个人喜好以及您想要如何表示数据。 例如,如果您只想要拥有给定产品的所有公司,您可以说:
或者
您的 SQL 查询返回产品信息以及公司。 如果这就是您想要的,您可以尝试:
如果您想提供更多信息,而不是创建另一个答案,请使用“添加评论”按钮。
You'll want to use the Entity Framework to set up a many-to-many mapping from Company to Product. This will use the CompanyProduct table, but will make it unnecessary to have a CompanyProduct entity set in your entity model. Once you've done that, the query will be very simple, and it will depend on personal preference and how you want to represent the data. For example, if you just want all the companies who have a given product, you could say:
or
Your SQL query returns the product information along with the companies. If that's what you're going for, you might try:
Please use the "Add Comment" button if you would like to provide more information, rather than creating another answer.
左外连接是通过使用实体框架中的 GroupJoin 来完成的:
http://msdn。 microsoft.com/en-us/library/bb896266.aspx
LEFT OUTER JOINs are done by using the GroupJoin in Entity Framework:
http://msdn.microsoft.com/en-us/library/bb896266.aspx
普通组连接表示左外连接。 试试这个:
该示例为您提供了
table1
中没有引用table2
的所有记录。如果省略
where
语句,您将获得table1
的所有记录。The normal group join represents a left outer join. Try this:
That example gives you all records from
table1
which don't have a reference totable2
.If you omit the
where
sentence, you get all records oftable1
.请尝试这样的事情:
干杯!
Please try something like this:
Cheers!
这个怎么样(您的实体设计器中的公司和产品之间确实存在多对多关系,不是吗?):
实体框架应该能够确定要使用的联接类型。
What about this one (you do have a many-to-many relationship between Company and Product in your Entity Designer, don't you?):
Entity Framework should be able to figure out the type of join to use.