C# IEnumerable<;i分组>;到IEnumerable<;列表>;

C# IEnumerable<;i分组>;到IEnumerable<;列表>;,c#,list,linq,igrouping,C#,List,Linq,Igrouping,所以我有这个: IEnumerable groupedObjects=myObjectsResults.GroupBy(x=>x.Id) 问题是,如何将此结果转换为IEnumerable 这是我所能做到的: IEnumerable groupedObjects=(myObjectsResults.GroupBy(x=>x.Id)。选择many(group=>group.ToList()) 这显然是错误的。有什么想法吗?IEnumerable groupedObjects=myObjectsRes

所以我有这个:

IEnumerable groupedObjects=myObjectsResults.GroupBy(x=>x.Id)

问题是,如何将此结果转换为
IEnumerable

这是我所能做到的:

IEnumerable groupedObjects=(myObjectsResults.GroupBy(x=>x.Id)。选择many(group=>group.ToList())

这显然是错误的。有什么想法吗?

IEnumerable groupedObjects=myObjectsResults.GroupBy(x=>x.Id)
IEnumerable<List<MyObject>> groupedObjects = myObjectsResults.GroupBy(x => x.Id)
                                            .Select(group => group.ToList())
                                            .ToList();
.Select(group=>group.ToList()) .ToList();
我认为解决方案更简单

iGroup是一个IEnumerable和IEnumerable

签名如下:

#region Assembly System.Core.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll
#endregion

using System.Collections;
using System.Collections.Generic;

namespace System.Linq
{
  // Summary:
  //     Represents a collection of objects that have a common key.
  //
  // Type parameters:
  //   TKey:
  //     The type of the key of the System.Linq.IGrouping<TKey,TElement>.This type
  //     parameter is covariant. That is, you can use either the type you specified
  //     or any type that is more derived. For more information about covariance and
  //     contravariance, see Covariance and Contravariance in Generics.
  //
  //   TElement:
  //     The type of the values in the System.Linq.IGrouping<TKey,TElement>.
  public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable
  {
    // Summary:
    //     Gets the key of the System.Linq.IGrouping<TKey,TElement>.
    //
    // Returns:
    //     The key of the System.Linq.IGrouping<TKey,TElement>.
    TKey Key { get; }
  }
}
#region Assembly System.Core.dll,v4.0.0.0
//C:\Program Files(x86)\Reference Assembly\Microsoft\Framework\.NETFramework\v4.5\System.Core.dll
#端区
使用系统集合;
使用System.Collections.Generic;
名称空间系统.Linq
{
//总结:
//表示具有公用键的对象的集合。
//
//类型参数:
//TKey:
//System.Linq.IGrouping.This类型的键的类型
//参数是协变的。也就是说,您可以使用指定的类型
//或任何更派生的类型。有关协方差和
//逆变,请参见泛型中的协方差和逆变。
//
//远程服务:
//System.Linq.iGroup中值的类型。
公共接口I分组:IEnumerable,IEnumerable
{
//总结:
//获取System.Linq.IGrouping的键。
//
//返回:
//系统的键为.Linq.i分组。
TKey{get;}
}
}

另一个舒适的解决方案是使用字典:

IEnumerable<IGrouping<UInt64, MyObject>> groupedObjects = myObjectsResults.GroupBy(x => x.Id);
Dictionary<UInt64, List<MyObject>> objectGroups = groupedObjects.ToDictionary(group => group.Key, group => group.ToList());
IEnumerable groupedObjects=myObjectsResults.GroupBy(x=>x.Id);
Dictionary objectGroups=groupedObjects.ToDictionary(group=>group.Key,group=>group.ToList());

我也有同样的问题,但我的收藏太多(数以百万计的元素),所以我不能打电话。托利斯,你有什么建议吗@red2nb只需使用
myObjectsResults.GroupBy(x=>x.Id)
,以后可以使用foreach循环对其进行迭代。(实际上,嵌套了两个foreach循环)