【发布时间】:2021-12-05 21:33:37
【问题描述】:
使用 .NET 6 我有以下内容:
List<String> values = new List<String?> { null, "", "value" }
.Where(x => !String.IsNullOrEmpty(x))
.Select(y => y)
.ToList();
但我收到了警告:
'string?[]' 类型的值中的引用类型的可空性与目标类型'string[]' 不匹配。
我认为使用
.Where(x => !String.IsNullOrEmpty(x))
可以解决问题,但不能。如何解决这个问题?
【问题讨论】:
-
.Cast<string>()会强迫它 - 但可能有更好的方法 -
.Select(y => y!)?