GroupBy a combination of columns - changes .Select
hi,
having bit of trouble lamda expression. i wish groupby combination of couple of columns , select single column. however whatever group seems going 'key'.
e.g. need group hour+timetype select hour
void main() { list<hours> hrs = gettimes(); hrs.dump(); list<string> hrlist = hrs .groupby (grp1 => string.format("{0}{1}" , grp1.timetype, grp1.hour)) .select (grp => grp.key.tostring()).tolist(); hrlist.dump(); } public class hours { public string hour { get; set; } public string timetype { get; set; } } public list<hours> gettimes() { list<hours> hrs = new list<hours>(); hrs.add(new hours() { timetype="am", hour="01"}); hrs.add(new hours() { timetype="am", hour="02"}); hrs.add(new hours() { timetype="am", hour="03"}); hrs.add(new hours() { timetype="am", hour="04"}); hrs.add(new hours() { timetype="am", hour="05"}); hrs.add(new hours() { timetype="pm", hour="01"}); hrs.add(new hours() { timetype="pm", hour="02"}); hrs.add(new hours() { timetype="pm", hour="03"}); return hrs; }
this gives me:
am01 |
am02 |
am03 |
am04 |
am05 |
pm01 |
pm02 |
pm03 |
the problem that, in groupby clase, creating single key, combination of timetype and hour fields. instead, must specify them separately:
list<string> hrlist = hrs .groupby(grp1 => new { grp1.timetype, grp1.hour }) .select(grp => grp.key.hour).tolist();
in way, in select lambda expression can specify hour value only.
marco minerva [mcpd]
blog: http://blogs.ugidotnet.org/marcom
twitter: @marcominerva
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment