This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18675|Reply: 1

[Source] LINQ left, right, and inner connections

[Copy link]
Posted on 8/9/2017 10:51:26 AM | | |

1. Left connection:
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                        
};

2. Right connection:
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
};

3. Internal connection:
var query = from t in entitiy. TB_GCGL_ADA_USER
                 join p in entitiy. TB_GCGL_ZY_ZYK
                 on t.ETPRS_CODE equals p.ETPRS_CODE
                 select new TB_USER_ZYK
                 {
                    USER_ID = t.USER_ID,
                    USER_NAME = t.USER_NAME,
                    USER_PASSWORD = t.USER_PASSWORD,
                 };



When making a left join between two tables, pay attention to the use of defaultifempty, which originally meant that if it is empty, use the default value instead, the default value is NULL, and of course, you can also use another overload of defaultifempty to specify the default.





Previous:dz database gbk to utf8 tool
Next:The detailed usage of createCriteria is the QBC query in Hibernate
Posted on 8/11/2017 6:28:43 PM |
linq sorts by multiple fields: service = service. OrderByDescending(s => s.LastUpdateDate). ThenByDescending(s => s.CreateDate);
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com