问题描述
将 IEnumerable<IGrouping<T,K>> 转换为 IEnumerable<K> 的最佳方法是什么?
看来这种方式可行:
myEnumerable = myEnumerableOfGroups .Select( group => group.AsEnumerable) .Aggregate((enumA, enumB) => enumA.Concat(enumB));
有没有更有效的方法?
推荐答案
应该这样做:
myEnumerable = myEnumerableOfGroups.SelectMany(group => group);
SelectMany 重载的 MSDN 页面是 这里.
问题描述
What is the best way to convert IEnumerable<IGrouping<T,K>> to IEnumerable<K> ?
It seems this way will work:
myEnumerable = myEnumerableOfGroups .Select( group => group.AsEnumerable) .Aggregate((enumA, enumB) => enumA.Concat(enumB));
is there a more efficient way to do it ?
推荐答案
This should do it:
myEnumerable = myEnumerableOfGroups.SelectMany(group => group);
The MSDN page for this overload of SelectMany is here.
相关问答
当T在运行时才知道时,将IEnumerable转换成IEnumerable<T>。
如何在C#中把IEnumerable<T>转换成List<T>?
ienumerable <t>转换
铸造/转换ienumerable <t>转换为iEnumerable <u>?
铸造/转换ienumerable <t>转换为iEnumerable <u>?
将连续的相同项目分组:iEnumerable <t> ienumerable <iEnumerable <t >>
将IEnumerable<T>转换为EntitySet<T>
转换/铸造iEnumerable <t>
将数组转换为iEnumerable <t>
将dataRowCollection转换为iEnumerable <t>