Linq查询中的空验证

时间:2015-08-12 13:57:04

标签: c# linq

@item.qmsCallAudit.Employee.EmployeeProcesses
     .Select(e=> e.Employee1.employee_code).SingleOrDefault().ToString();

如何检查e.Employee1.employee_code的空值

2 个答案:

答案 0 :(得分:0)

你可以这样做:

var result = item.qmsCallAudit.Employee.EmployeeProcesses
 .Where(e => e.Employee1 != null && e.Employee1.employee_code != null)
 .Select(e=> e.Employee1.employee_code)
 .SingleOrDefault();

var code = result != null ? result.ToString() : null;

ToString()之后SingleOrDefault()不能返回null,然后会抛出异常。

答案 1 :(得分:0)

@item.qmsCallAudit.Employee.EmployeeProcesses.Where(e => e.Employee1.employee_code != null).Select(e => e.Employee1.employee_code).SingleOrDefault().ToString();

试试这个^^