EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询
来源:互联网 发布:海口百度最seo 编辑:程序博客网 时间:2024/04/30 09:40
场景:在实际的项目中使用EntityFramework都会遇到使用Ef处理连接查询的问题,这里做一些小例子如何通过Linq语法处理内连接(inner join)、外连接(left/right outer join);
废话不多说先看实体类:
1.内连接:
Linq:
var query = from st in context.SchoolBoys join gl in context.SchoolGirls on st.GirlfriendId equals gl.Id select new { Id = st.Id, Name = st.Name, GirlfriendName = gl.Name };
sql:
SELECT 1 AS [C1], [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name], [Extent2].[Name] AS [Name1] FROM [dbo].[SchoolBoys] AS [Extent1] INNER JOIN [dbo].[SchoolGirls] AS [Extent2] ON [Extent1].[GirlfriendId] = [Extent2].[Id]
2.外连接(这里只介绍左外连接)
Linq:
var query = from st in context.SchoolBoys join cl in context.Classes on st.ClassId equals cl.Id into cls from c in cls.DefaultIfEmpty() select new { Id = st.Id, Name = st.Name, ClassName = c.Name };
sql:
SELECT 1 AS [C1], [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name], [Extent2].[Name] AS [Name1] FROM [dbo].[SchoolBoys] AS [Extent1] LEFT OUTER JOIN [dbo].[Classes] AS [Extent2] ON [Extent1].[ClassId] = [Extent2].[Id]
3.多表混合
linq:
var query3 = from st in context.SchoolBoys join gl in context.SchoolGirls on st.GirlfriendId equals gl.Id into sgs from sg in sgs join cl in context.Classes on sg.ClassId equals cl.Id into cls from cla in cls.DefaultIfEmpty() join g in context.Grades on cla.GradeId equals g.Id into gs from gr in gs.DefaultIfEmpty() select new { Id = st.Id, Name = st.Name, ClassName = cla.Name, GradeName = gr.Name };
sql:
SELECT 1 AS [C1], [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name], [Extent3].[Name] AS [Name1], [Extent4].[Name] AS [Name2] FROM [dbo].[SchoolBoys] AS [Extent1] INNER JOIN [dbo].[SchoolGirls] AS [Extent2] ON [Extent1].[GirlfriendId] = [Extent2].[Id] LEFT OUTER JOIN [dbo].[Classes] AS [Extent3] ON [Extent2].[ClassId] = [Extent3].[Id] LEFT OUTER JOIN [dbo].[Grades] AS [Extent4] ON [Extent3].[GradeId] = [Extent4].[Id]
0 0
- EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询
- 内连接<inner join>,左外连接<left outer join>,右外连接<right outer join>
- Linq表连接大全(INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN、CROSS JOIN)
- 区别交叉连接(cross jon) 内连接(inner join) 和外连接(left outer join,right outer join)
- Linq语句实现(INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN、CROSS JOIN)
- sql多表连接查询inner join, left join , right join ,full join ,cross join
- sql多表连接查询inner join, left join , right join ,full join ,cross join
- sql多表连接查询inner join, left join , right join ,full join ,cross join
- sql多表连接查询inner join, left join , right join ,full join ,cross join
- sql多表连接查询inner join, left join , right join ,full join ,cross join
- sql 多表连接查询inner join, left join , right join ,full join ,cross join
- mysql多表连接查询inner join, left join , right join ,full join ,cross join
- sql多表连接查询inner join, left join , right join ,full join ,cross join
- SQL多表连接查询INNER JOIN, LEFT JOIN , RIGHT JOIN ,FULL JOIN ,CROSS JOIN
- sql多表连接查询inner join, left join , right join ,full join ,cross join
- sql多表连接查询inner join, left join , right join ,full join ,cross join
- SQL优化--使用内连接(inner join)代替外连接(left join,right join)
- Oracle中内连接查询(inner join),自然连接查询(natural join),自连接查询,外连接查询(outer join),左外连接(left join),右外连接(right joi
- CentOS7安装mysql
- Spring-RequestBody映射MAP结构
- 越长大,越孤单
- Linux防火墙iptables学习笔记(二)参数指令
- 关于php的加密函数
- EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询
- ATM 自动存款机 (简易代码)
- wireshark部分过滤规则
- Mysql存储过程中字符串分割
- VirtualBox共享文件夹
- Appium测试时如何关联到Genymotion模拟器
- js拼图游戏
- 使用Netty4实现基本的消息分发
- TabLayout属性详解