Linq,如何检查字段的值是否为null

时间:2012-02-08 13:28:13

标签: asp.net linq null nullreferenceexception

使用linq查询时我收到错误

  

System.NullReferenceException:对象引用未设置为   对象的实例。

var db = from d in DepartmentBLL.GetDepartmentList()
     join b in BudgetMasterBLL.GetBudgetMasterList()            
     on d.Departmentid equals b.Departmentid into leftJoin                     
     from results in leftJoin.DefaultIfEmpty()
     select new
     {
         Name = d.Name,
         Create = results.Budgetmasterid == null ? "null": "value", //ERROR HERE
         CreateURL = "frmBudgetInitial.aspx?departmentid=" + d.Departmentid.ToString() + "&departmentcategoryid=" + d.Departmentcategoryid.ToString()
     };

我在论坛上找到了一些帮助,但没有解决我的问题,请指教。感谢。,

2 个答案:

答案 0 :(得分:3)

Create = results == null || string.IsNullOrEmpty(results.Budgetmasterid) ? "null": "value",

Create = results == null || string.IsNullOrWhitespace(results.Budgetmasterid) ? "null": "value",

答案 1 :(得分:2)

这应该有帮助

Create = results == null || results.Budgetmasterid == null ? "null": "value", //ERROR HERE