我有以下代码:
IEnumerable<DestinationResult> destinations =
_repository.GetDestinationData();
IEnumerable<Destination> finalDestinations =
destinations.Select(GetAdditionalDestinationInfo);
private Destination GetAdditionalDestinationInfo(DestinationResult d){ /* CODE */}
我如何仍然使用方法组调用(Select(GetAdditionalDestinationInfo))并过滤掉可能从GetAdditionalDestinationInfo返回的空值(无需再次调用该方法来检查where子句中的null)。
类似的东西:
IEnumerable<Destination> finalDestinations =
destinations.Select(GetAdditionalDestinationInfo != null)
答案 0 :(得分:4)
Select
将输入记录逐个映射到输出记录 - 没有机会进行过滤。相反,您想要添加另一个Where
:
IEnumerable<Destination> finalDestinations =
destinations
.Select(GetAdditionalDestinationInfo)
.Where(i => i != null);