发布网友 发布时间:2022-04-07 19:50
共3个回答
懂视网 时间:2022-04-08 00:11
var list = (from c in customerDb.Order 2 join o in customerDb.OrderItem on c.OrderId equals o.OrderItemId 3 select new { c = c.OrderId }).FirstOrDefault();生成的Sql
1 SELECT TOP (1) 2 [Extent1].[OrderId] AS [OrderId] 3 FROM [dbo].[Orders] AS [Extent1] 4 INNER JOIN [dbo].[OrderItems] AS [Extent2] ON [Extent1].[OrderId] = [Extent2].[OrderItemId]
Left Join
Linq:
1 var list = (from c in customerDb.Order 2 join o in customerDb.OrderItem on c.OrderId equals o.OrderItemId 3 into Grp 4 from grp in Grp.DefaultIfEmpty() 5 select new { c = c.OrderId, grp = grp.OrderItemId }).FirstOrDefault();
生成的Sql:
1 SELECT TOP (1) 2 [Extent1].[OrderId] AS [OrderId], 3 [Extent2].[OrderItemId] AS [OrderItemId] 4 FROM [dbo].[Orders] AS [Extent1] 5 LEFT OUTER JOIN [dbo].[OrderItems] AS [Extent2] ON [Extent1].[OrderId] = [Extent2].[OrderItemId]
Cross Join
Linq:
1 var list = (from c in customerDb.Order 2 from o in customerDb.OrderItem 3 select new { c = c.OrderId,o=o.OrderItemId }).FirstOrDefault();
生成的Sql:
1 SELECT TOP (1) 2 [Extent1].[OrderId] AS [OrderId], 3 [Extent2].[OrderItemId] AS [OrderItemId] 4 FROM [dbo].[Orders] AS [Extent1] 5 CROSS JOIN [dbo].[OrderItems] AS [Extent2]
总结:右连接其实就是换下两个表的顺序位置,
全连接就是把左外连接的结果拼接起来去重就行。
用顺ef后,可以加速开发效率,期待那一天早日到来,加油啊!!!
Linq实现t-Sql的各种连接
标签:
热心网友 时间:2022-04-07 21:19
LINQ 代码链接数据库是由系统自动生成的额,链接字符串代码在xxxxx.designer.cn里面。还有linq就是为了方便手动拖哈热心网友 时间:2022-04-07 22:37
用LINQ不就免了这个麻烦了,拖拖不是很简单吗