IEnumerable <T>到IDictionary <U,IEnumerable <T >>

Rem*_*mus 3 .net c# ienumerable type-conversion idictionary

什么是最有效的方式转换IEnumerable<T>IDictionary<U, IEnumerable<T>>

其中U是例如Guid,其信息保存在T的属性中.

基本上,这会创建一个列表字典,其中原始列表中的所有项目都是根据对象内属性中的值进行分组的.

对象定义:

class myObject
{
    public Guid UID { get; set; }

    // other properties
}
Run Code Online (Sandbox Code Playgroud)

从...开始:

IEnumerable<myObject> listOfObj;
Run Code Online (Sandbox Code Playgroud)

结束于:

IDictionary<Guid, IEnumerable<myObject>> dictOfLists;
Run Code Online (Sandbox Code Playgroud)

Whereby listOfObj包含具有许多不同但有时重叠的UID属性值的对象.

Fem*_*ref 5

使用LINQ:

var dict = input.GroupBy(elem => elem.Identifier)
                .ToDictionary(grouping => grouping.Key, grouping => grouping.Select(x => x));
Run Code Online (Sandbox Code Playgroud)