您如何在Linq中对实体进行多个内部联接 [英] How do you do Multiple Inner Joins in Linq to Entities

查看:38
本文介绍了您如何在Linq中对实体进行多个内部联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了SO,因此无法为此找到可行的解决方案.我只是想弄清楚Linq to Entities中多个内部联接的语法是什么.谢谢

I have already searched through SO and could not fins a workable solution for this. I am just trying to figure what is the syntax for multiple inner joins in Linq to Entities. Thanks

推荐答案

乔恩的答案会起作用,但恕我直言在LINQ to Entities中使用join通常是错误的,因为它会复制模型中的代码.我可以在L2E中以更简单的方式重写Jon的查询:

Jon's answer will work, but IMHO using join in LINQ to Entities is usually wrong, because it duplicates code in your model. I can rewrite Jon's query in a much simpler way in L2E:

var query = from customer in db.Customers
            from order in customer.Orders
            from product in order.Products
            from info in product.Info
            select new
            {
                customer.Name, 
                info.BriefDescription
            }

大约是打字的50%,是重复代码的0%.考虑到您的关系已经在数据库和模型中定义.您是否真的要在您编写的每个查询中再次复制它们,并在重构模型时中断查询?

That's about 50% of the typing and 0% of the duplicated code. Consider that your relationships have already been defined in your DB and in your model. Do you really want to duplicate them again in every query you write, and break your queries when you refactor your model?

这篇关于您如何在Linq中对实体进行多个内部联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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