Check for null entities in EF statement

Viewed 69

I have a statement like this:

AssignedCas AssignedCase in caseList.GroupBy(o => o.CaseBatch.CaseBatchName)
                                    .Select(g => g.First()).ToList()

But at time o.CaseBatch.CaseBatchName can be null, in which case the above statement will not execute.

Is there a way in the above statement to check if CaseBatchName is not null and only then include it.

1 Answers

You should probably just add a where condition in your Linq. Just like this:

AssignedCas AssignedCase in caseList.Where(o => o.CaseBatch.CaseBatchName != null)
                                    .GroupBy(o => o.CaseBatch.CaseBatchName)
                                    .Select(g => g.First()).ToList()
Related