c# - IEnumerable<T> to IDictionary<U, IEnumerable<T>> -


what's efficient way convert ienumerable<t> idictionary<u, ienumerable<t>>

where u is, example guid, information held in property of t.

basically, creates dictionary of lists items in original list grouped based on value in property within objects.

example

object definition:

class myobject {     public guid uid { get; set; }      // other properties } 

start with:

ienumerable<myobject> listofobj; 

end with:

idictionary<guid, ienumerable<myobject>> dictoflists; 

whereby listofobj contains objects have many different, overlapping values uid property.

using linq:

var dict = input.groupby(elem => elem.identifier)                 .todictionary(grouping => grouping.key, grouping => grouping.select(x => x)); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -