c# - Passing an expression as a parameter -


i'm trying build generic groupby method, guess should this

var result = genericgroupby<list<dto>>(datalist, g=>g.id);  public object genericgroupby<t>(object data, func<t, bool> groupbyexpression) {     return ((list<t>)data).groupby(groupbyexpression); } 

but can not make work.

how pass expression g=>g.id?

currently there 2 problems:

  • your method expects func<t, bool> , suspect g => g.id fails because id property isn't bool
  • you're specifying list<dto> type argument, when suspect want dto.

given comments, work:

var result = genericgroupby<dto>(datalist, g => g.id);  public object genericgroupby<t>(object data, func<t, int> groupbyexpression) {     return ((list<t>)data).groupby(groupbyexpression); } 

... i'd make bit more general unless always want group int:

var result = genericgroupby<dto, int>(datalist, g => g.id);  public object genericgroupby<telement, tkey>     (object data, func<telement, tkey> groupbyexpression) {     return ((ienumerable<telement>)data).groupby(groupbyexpression); } 

note how i've changed cast list<t> ienumerable<t> - don't need list<t>, why cast that?


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -