且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将空值转换为空IEnumerable T的Linq方法

更新时间:2023-02-14 11:32:38

这实际上与您拥有的解决方案相同,但是我使用了扩展方法.

This is in effect the same solution that you have, but I use an extension method.

public static partial class EnumerableExtensions
{
    public static IEnumerable<T> EmptyIfNull<T>(this IEnumerable<T> source)
    {
        return source ?? Enumerable.Empty<T>();
    }
}

这样我们最终得到:

IEnumerable<Thing> procs = APICall(foo, bar);
var result = from proc in procs.EmptyIfNull()
             where proc != null
             select Transform(proc);