Say I have an arbitrary number of collections, each containing objects of the same type (for example, List<int> foo
and List<int> bar
). If these collections were themselves in a collection (e.g., of type List<List<int>>
, I could use SelectMany
to combine them all into one collection.
However, if these collections are not already in the same collection, it's my impression that I'd have to write a method like this:
public static IEnumerable<T> Combine<T>(params ICollection<T>[] toCombine)
{
return toCombine.SelectMany(x => x);
}
Which I'd then call like this:
var combined = Combine(foo, bar);
Is there a clean, elegant way to combine (any number of) collections without having to write a utility method like Combine
above? It seems simple enough that there should be a way to do it in LINQ, but perhaps not.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…