我在 linq to sql 中进行左连接,所以我的问题是在选择正确的表字段时,我正在检查每个字段,无论连接的对象是否为空,这是正确的方法吗?还是有其他方法可以做到这一点?我的查询就像
from u in user
join x in employeee on u.id equals x.userId
into ux from ujoinx in ux.DefaultIfEmpty()
join y in department on x.id equals y.employeeId
into xy from xjoiny in xy.DefaultIfEmpty()
select new {
EmployeeSal = ujoinx!=null?ujoinx.employeeSal:0, // see checkig for null
EmployeeTax = ujoinx!=null?ujoinx.employeeTax:0, // in this 3 lines
UserName = u.username,
DeptName = xjoiny!=null?xjoiny.name:"" //is this a correct way ?
}
查询正确地产生了答案,但如果我不检查这几个字段是否为空,它就会抛出object reference not set.....error
。这到底是DefaultIfEmpty()
做什么的?