?? Lambda LINQ中的运算符

Căt*_*doi 0 c# linq

results = results.Where(x => x.SomeValue ?? 0 == 0);
Run Code Online (Sandbox Code Playgroud)

我想检查一个值是否为null,如果为null,则将其设为零,然后进行比较.

我不知道怎么把额外的括号()

错误信息是:

Ser*_*kiy 7

results = results.Where(x => (x.SomeValue ?? 0) == 0);
Run Code Online (Sandbox Code Playgroud)

但是如果你处理可空类型,我发现更可读的显式检查为null:

results = results.Where(x => !x.SomeValue.HasValue || x.SomeValue == 0);
Run Code Online (Sandbox Code Playgroud)

另一个选项是GetValueOrDefault()method,它将为整数(long,byte)返回零:

results = results.Where(x => x.SomeValue.GetValueOrDefault() == 0);
Run Code Online (Sandbox Code Playgroud)