加入3表

编程入门 行业动态 更新时间:2024-10-27 06:21:59
本文介绍了加入3表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想通过LINQ加入3表,我这样做了: var results = northwind.Products.Join(northwind.Categories,c => c.CategoryID,d => d.CategoryID ,(c,d)=> new {c.ProductID,c.ProductName,c.SupplierID,d.CategoryName}) .Where(q => q.ProductID> 12)。 (northwind.Suppliers,f => f.SupplierID,g => g.SupplierID,(f,g) => new {f.CategoryName,f.ProductName,g.CompanyName,g.ContactName, g.Country}); 但查询太长了,谁能告诉我写得更短? 谢谢。

解决方案

这是一种不同的编写方式,它更简洁,但最终两种方式在后端产生相同的SQL。

SELECT [t2]。[CategoryName],[t0]。[ProductName],[t1]。[CompanyName],[t1 ]。[ContactName],[t1]。[Country]

FROM [dbo]。[Products] AS [t0]

INNER JOIN [dbo]。[供应商] AS [t1] ON [t0]。[SupplierID] =([t1]。[SupplierID])

INNER JOIN [dbo]。[Categories] AS [t2] ON [t0]。[CategoryID] =([t2]。[CategoryID])

WHERE [t0]。[ProductID]> 12 var qresult = from p in db .Products

其中 p.ProductID> ; 12

join s in db.Suppliers on p.SupplierID equals s.SupplierID

join c in db.Categories on p.CategoryID equals c.CategoryID

选择 new {c.CategoryName,p.ProductName,s.CompanyName,s.ContactName,s.Country};

I want to join 3 table by LINQ , i did this : var results = northwind.Products.Join(northwind.Categories, c => c.CategoryID, d => d.CategoryID, (c, d) => new { c.ProductID, c.ProductName, c.SupplierID, d.CategoryName })                                            .Where(q => q.ProductID > 12)                                            .Join(northwind.Suppliers, f => f.SupplierID, g => g.SupplierID, (f, g) => new { f.CategoryName, f.ProductName, g.CompanyName, g.ContactName, g.Country });but the query is too long, who can tell me to write it shorter ?thank.         

解决方案

This is a different way to write it which is a little cleaner, but in the end both ways produce the same exact SQL on the back end.

SELECT [t2].[CategoryName], [t0].[ProductName], [t1].[CompanyName], [t1].[ContactName], [t1].[Country]

FROM [dbo].[Products] AS [t0]

INNER JOIN [dbo].[Suppliers] AS [t1] ON [t0].[SupplierID] = ([t1].[SupplierID])

INNER JOIN [dbo].[Categories] AS [t2] ON [t0].[CategoryID] = ([t2].[CategoryID])

WHERE [t0].[ProductID] > 12var qresult = from p in db.Products

where p.ProductID > 12

join s in db.Suppliers on p.SupplierID equals s.SupplierID

join c in db.Categories on p.CategoryID equals c.CategoryID

select new {c.CategoryName, p.ProductName, s.CompanyName, s.ContactName, s.Country };

更多推荐

加入3表

本文发布于:2023-10-17 06:24:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500035.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!