0

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);
}
BumbleBee
  • 10,429
  • 20
  • 78
  • 123

1 Answers1

3

Try with this condition in your Where method:

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

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,

ocuenca
  • 38,548
  • 11
  • 89
  • 102