你如何在Linq中实现多个内连接

时间:2021-10-11 04:19:26

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

我已经搜索过SO并且无法为此找到可行的解决方案。我只想弄清楚Linq to Entities中多个内连接的语法是什么。谢谢

2 个解决方案

#1


9  

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:

Jon的答案会起作用,但是使用LINQ to Entities连接的IMHO通常是错误的,因为它复制了模型中的代码。我可以在L2E中以更简单的方式重写Jon的查询:

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
            }

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?

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

#2


7  

Well, I don't know LINQ to Entities particularly, but the normal LINQ syntax would be:

好吧,我不太了解LINQ to Entities,但正常的LINQ语法是:

var query = from customer in db.Customers
            join order in db.Orders on customer.ID equals order.ID
            join product in db.Products on order.ProductID equals product.ID
            join info in db.Info on product.InfoID equals info.ID
            select new { customer.Name, info.BriefDescription };

(i.e. just several join clauses).

(即只有几个连接条款)。

Now I suspect that you've already tried that - if so, what went wrong?

现在我怀疑你已经尝试过了 - 如果是的话,出了什么问题?

#1


9  

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:

Jon的答案会起作用,但是使用LINQ to Entities连接的IMHO通常是错误的,因为它复制了模型中的代码。我可以在L2E中以更简单的方式重写Jon的查询:

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
            }

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?

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

#2


7  

Well, I don't know LINQ to Entities particularly, but the normal LINQ syntax would be:

好吧,我不太了解LINQ to Entities,但正常的LINQ语法是:

var query = from customer in db.Customers
            join order in db.Orders on customer.ID equals order.ID
            join product in db.Products on order.ProductID equals product.ID
            join info in db.Info on product.InfoID equals info.ID
            select new { customer.Name, info.BriefDescription };

(i.e. just several join clauses).

(即只有几个连接条款)。

Now I suspect that you've already tried that - if so, what went wrong?

现在我怀疑你已经尝试过了 - 如果是的话,出了什么问题?