I am trying to use the below line of code to extract necessary data from a list and ingest into a different list. Most of the time I am able to get this process success. But at some point, I am getting a null reference exception. I did check for null in the first place for the source list.
List<Employees> marketingEmployeeList = new List<Employees>();
if (totalEmployeeList != null && totalEmployeeList.Any())
{
marketingEmployeeList = totalEmployeeList.Where(x => x.department == "Marketing").ToList();
}
If(marketingEmployeeList.Any())
{
CallMethod(marketingEmployeeList);
}
My stack trace contains the below
at System.Linq.Enumerable.WhereListIterator
1.ToList() at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source)
Previously I didnot check null for totalEmployeeList. I got the null error But later even after i checked null I get the null reference error