假设我有任意数量的集合,每个集合包含相同类型的对象(例如,List<int> foo和List<int> bar).如果这些集合本身就是一个集合(例如,类型List<List<int>>,我可以使用SelectMany它们将它们全部合并到一个集合中.
但是,如果这些集合不在同一个集合中,那么我的印象是我必须编写这样的方法:
public static IEnumerable<T> Combine<T>(params ICollection<T>[] toCombine)
{
return toCombine.SelectMany(x => x);
}
Run Code Online (Sandbox Code Playgroud)
然后我会这样称呼:
var combined = Combine(foo, bar);
Run Code Online (Sandbox Code Playgroud)
是否有一种干净,优雅的方式来组合(任意数量)集合而无需编写Combine如上所述的实用方法?看起来很简单,应该有一种方法在LINQ中做到,但也许不是.