opencall.Priority =
averages.Where(x => x.ProblemCode == opencall.ProblemCode)
.SingleOrDefault().Priority;
The above lambda statement returns some nulls because ProblemCode isn't always guaranteed to be in the averages list.
How can I rewrite this statement so that if that is the case opencall.Priority is set to "" instead of the application throwing an error?
Try getting the problem code first, then check if it's null.
You can simply write:
Assuming Priority is string, you could try:
Split it up:
You have to provide a new default value for your reference type, other than
null
.So
Priority
is astring
? Note that you don't needSingleOrDefault
anymore since the query can never throw an exception because it is empty when you provide aDefaultIfEmpty
.