I have fetched the List<> object as below (with .Include()):
List<vDetail> entityvDetails =
context.vDetails
.Include("payInstallment.appsDetail")
.Include("payInstallment.appsDetail.application")
.Include("payInstallment.appsDetail.purposes")
.Where(e => e.vch_id == 123).ToList();
And then somewhere in the code ahead I tried to filter the entity record as below:
foreach (vDetail item in lstVDetails)
{
...
int purposeId = entityvDetails.Where(e => e.sad_id == item.sad_id).FirstOrDefault().payInstallment.appsDetail.purposes.prp_id;
...
}
Code compiling perfect. However, the runtime returning following error (although all navigations are included):
Object reference not set to an instance of an object.
So I set for debugging using the watch window. Now while analyzing the below statement in watch window:
entityVoucherDetails.Where(e => e.sad_id == item.sad_id).FirstOrDefault()
the watch window generated following error:
Expression cannot contain lambda expressions.
Please if anybody can tell me what could be the reason?
FirstOrDefaultexplicitely allows nullrefs. If you are sure its never null useFirst. Do more checks for nullref over multiple lines before assuming something is not null. Will also help with debugging this. Assign the result ofentityvDetails.Where(e => e.sad_id == item.sad_id).FirstOrDefault()to a dedicated variable.