zoukankan      html  css  js  c++  java
  • EntityFramework 使用Linq处理内连接(inner join)、外链接(left/right outer join)、多表查询

    场景:在实际的项目中使用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]

    欢迎指正 转载请注明出处:http://www.cnblogs.com/xinwang/p/6145837.html

  • 相关阅读:
    C语言中typedef可以出现在struct定义之前
    C语言编程规划——怎样用C语言设计一个程序
    如何去掉页眉页脚上的横线 如何去掉页眉上的横线 如何去掉页脚上的横线
    在Visual Studio 2005中使用WinPcap编写程序的配置方法
    使用WinPcap编程(1)——获取网络设备信息
    如何思考多维数组
    Visual Studio 2005编写C程序出错:error C2143,)前缺少*
    在Linux下运行peersim1.0.5
    Login failed for user 'IIS APPPOOL\DefaultAppPool'
    win2008 r2 安装sqlserver2005 sp3报错
  • 原文地址:https://www.cnblogs.com/xinwang/p/6145837.html
Copyright © 2011-2022 走看看