有Linq方法可以.SelectMany(x=>;x)

本文关键字:gt 方法 Linq SelectMany | 更新日期: 2023-09-27 18:26:37

我需要压平一个相同类型的可枚举的可枚举。在FP中,它将是标准函数concat(http://msdn.microsoft.com/en-us/library/ee353462.aspx)。我在C#(Linq)中做什么?

有Linq方法可以.SelectMany(x=>;x)

假设您的意思是"不必指定x => x":否-但您可以写一个

public static IEnumerable<T> Flatten<T>(this IEnumerable<IEnumerable<T>> source) {
    return source.SelectMany(x => x);
}

edit:但为了一致性,也许无参数的SelectMany是一个更清晰的名称:

public static IEnumerable<T> SelectMany<T>(this IEnumerable<IEnumerable<T>> source) {
    return source.SelectMany(x => x);
}

IEnumerable.SelectMany(x => x.ToList())应该为您做到这一点。