c# - List<Info> where Info has Last and First properties map to Dictionary<Last,List<First>> -


if have class of type

class info {     public info(string first, string last) { this.first = first; this.last = last; }     string first { get; private set; }     string last { get; private set; } } 

and list example:

var list = new list<info>(); list.add(new info("jon", "doe"); list.add(new info("jane", "doe"); list.add(new info("bason", "borne"); list.add(new info("billy", "nomates"); 

and want map in sorted list of last name, list of firstnames, i.e. dictionary<string,list<string>> key last name property, , list list of first name properties.

in example above { "doe" => { "jon", "jane" }, "borne" => { "jason" }, "nomates" => { "billy" } }.

is possible neatly linq , if how go this?

you can use groupby , todictionary done:

first, make properties in class public, use code:

dictionary<string, list<string>> names = list     .groupby(x => x.last)     .todictionary(x => x.key, x => x.select(y => y.first).tolist()); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -