更新时间:2023-11-26 08:18:28
您可以使用DefaultIfEmpty
为空集合指定自定义默认值:
You can use DefaultIfEmpty
to specify a custom default value for an empty collection:
var value = numbers.Where(MyFilter)
.DefaultIfEmpty(-1) //or any other default value you want
.Single(); //OrDefault not needed; DefaultIfEmpty ensures there is an item