linq的左连接右连接内连接用法
1、左连接:
var LeftJoin = from e in ListOfEmployees
join d in ListOfDepartment
on e.DeptID equals d.ID into JoinedEmpDept
from d in JoinedEmpDept.DefaultIfEmpty()
select
new
{
EmployeeName = e.Name,
DepartmentName = d!= null ? d.Name :
null
};
2、右连接:
var RightJoin = from d in
ListOfDepartment
join e in ListOfEmployees
on d.ID equals e.DeptID into joinDeptEmp
from e in joinDeptEmp.DefaultIfEmpty()
select
new
{
EmployeeName = e!= null ? e.Name : null,
DepartmentName = d.Name
};
3、内连接:
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,
};
三个表链表查询
var list = (
from u in dc.userinfos
join j in dc.jobs on u.job equals j.jid into j_join
from x in j_join.DefaultIfEmpty()
join c in dc.cities on u.city equals c.cid into c_join
from v in c_join.DefaultIfEmpty()
select new
{
id = u.id,
name = u.name,
jname = x.jname,
cname = v.cname,
/*u1=u,x1=x,v1=v*/
//不要用对象的方式 因为对象可能为null那么对象.属性就会抛异常
}
).ToList();