zoukankan      html  css  js  c++  java
  • linq to sql 实现左(右)连接,那个方法是对的,该怎么处理

    linq to sql 实现左(右)连接,那个方法是对的
    var query2 = from tb0 in db.table_0
      join tb1 in db.table_1 on table_0.关联键 equals table_1.关联键
      into all
      from tb2 in all.DefaultIfEmpty()
      select new { ... };
    下面是另一方法。。。。。。

    左连接left outer join,除了满足连接条件的行,还包括左表的所有行。
    右连接right outer join,除了满足连接条件的行,还包括右表的所有行。
    eg:var q = 
      from e in db.Employees 
      join o in db.Orders on e equals o.Employee into ords 
      select new 
      { 
      e.FirstName, 
      e.LastName, 
      Order = ords
      }; 
    说明:以Employees 左表,Orders右表,Orders 表中为空时,用null值填充。Join的结果重命名 ords,Order是一个集合。 
      虽然没有left关键字,但是实现的查询结果是一样的。

    ------解决方案--------------------
    直接join是内连接。
    ------解决方案--------------------
    它产生的sql语句是什么呢?
    ------解决方案--------------------

    C# code
    左连接:
    
    var LeftJoin = from emp in ListOfEmployees
    join dept in ListOfDepartment
    on emp.DeptID equals dept.ID into JoinedEmpDept 
    from dept in JoinedEmpDept.DefaultIfEmpty()
    select new                          
    {
    EmployeeName = emp.Name,
    DepartmentName = dept != null ? dept.Name : null                          
    };
    
     
    
    右连接:
    
    var RightJoin = from dept in ListOfDepartment
    join employee in ListOfEmployees
    on dept.ID equals employee.DeptID into joinDeptEmp
    from employee in joinDeptEmp.DefaultIfEmpty()
    select new                            
    {
    EmployeeName = employee != null ? employee.Name : null,
    DepartmentName = dept.Name
    };
    
  • 相关阅读:
    Java异常处理和设计
    一次qps测试实践
    Alternate Task UVA
    Just Another Problem UVA
    Lattice Point or Not UVA
    Play with Floor and Ceil UVA
    Exploring Pyramids UVALive
    Cheerleaders UVA
    Triangle Counting UVA
    Square Numbers UVA
  • 原文地址:https://www.cnblogs.com/weixing/p/4447726.html
Copyright © 2011-2022 走看看