'How can i do a check for nulls in a lambda expression in c#? [duplicate]
I have the following but some of the fields are null and causes an error:
Value cannot be null.
Parameter name: source
at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)
foreach (var value in users.entries.Where(x => (x.field1 || x.field2 || x.field3) &&
!x.field4 && !x.field5))
{
//code
}
How can i do a check for nulls in the following expression , so check for the fields , field1,field2, field3,field4,field5 ?
Solution 1:[1]
looking from the error its likely your users.entries was null so you could not call Where on it.
Value cannot be null. Parameter name: source
Here source is the first parameter of the Where(...) extension
try this
if (users?.entries is null) {
// handle null
}
else {
foreach ...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Madushan |
