簡體   English   中英

是否有執行.SelectMany(x => x)的Linq方法?

[英]Is there a Linq method that does .SelectMany(x => x)?

我需要展平一個相同類型的可枚舉。 在FP中,它將是一個標准功能concathttp://msdn.microsoft.com/zh-cn/library/ee353462.aspx )。 我在C#(Linq)中做什么?

假設您的意思是“無需指定x => x ”:否-但您可以只寫一個

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

編輯:但是也許沒有參數SelectMany是一個更清晰的名稱,以保持一致性:

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

IEnumerable.SelectMany(x => x.ToList())應該為您執行此操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM