“对象引用未设置为对象的实例”尝试获得计数时出错 [英] "Object reference not set to an instance of an object" error while trying to get the count

查看:177
本文介绍了“对象引用未设置为对象的实例”尝试获得计数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有企业 EmployeeEvaluations EF类。我执行一个左外连接,当我使用LINQ语句来获取计数我收到以下错误,即使我正在检查null:

I have Corporates and EmployeeEvaluations EF classes. I am performing a Left outer join and when I use the LINQ statement to get the count I am getting the following error even though I am checking for null :


对象引用未设置为对象的实例。

Object reference not set to an instance of an object.



 CompanyBO result = new CompanyBO();
            try
            {
                using (CompEntities db = new CompEntities())
                var res = await(from c in db.Corporates                                                   
                 join ee in db.EmployeeEvaluations on esj.Id equals 
                 ee.EmployeeId into eels
                 from eelsj in eels.DefaultIfEmpty()
                 select new { corp = c,  empEvals = eelsj }
                  ).ToListAsync();

result.Qualified = res.Where(a => a.empEvals.EvaluationStatusId != null).Count(a => a.empEvals.EvaluationStatusId == 3);
}


推荐答案

方法:

result.Qualified = res.Where(a => a.empEvals != null).Count(a => a.empEvals.EvaluationStatusId == 3);

问题是如果 eels 是空的您将在 empEvals 中获得 null 值,这就是为什么您需要检查您的匿名类型中的属性是 null 或不在您的最后一个查询

The problem is if eels is empty then you will get a null value in the empEvals, that's why you need to check if that property in your anonymous type is null or not in your last query,

这篇关于“对象引用未设置为对象的实例”尝试获得计数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆