c# - add dynamic anonymous object into linq groupBy expression -


i have following linq expression:

func<entity, object> groupquery = item =>       new { = item.attributes["name"], item = item.attributes["number"] }; var result = target.collection.entities.groupby(groupquery).tolist(); 

but if don't know, how columns group (for example 3 instead of 2),and names of attributes stored in list names, how should change groupquery object? first idea create dynamic object don't work

dynamic groupquery= new expandoobject(); idictionary<string, object> dictionary = (idictionary<string, object>)groupquery;  foreach (string str in names) {    dictionary.add(str, str); } 

instead of returning object groupquery can return string. string constructed properties of objects want group. depending on configuration can generated in different ways i.e. based on different properties. here code shows idea:

public class {     public string property1 { get; set; }     public string property2 { get; set; }     public string property3 { get; set; } }  public enum groupbyumode {     groupby1,     groupby2,     groupby3, }  ...  var list = new list<a>(); (int = 0; < 10; ++i)     (int j = 0; j < 10; ++j)         (int k = 0; k < 10; ++k)             list.add(new { property1 = i.tostring(), property2 = j.tostring(), property3 = k.tostring() });  var mode = groupbyumode.groupby1;  func<a, object> func = => {     if (mode == groupbyumode.groupby1)         return a.property1;     else if (mode == groupbyumode.groupby2)         return string.format("{0}_{1}", a.property1, a.property2);     else if (mode == groupbyumode.groupby3)         return string.format("{0}_{1}_{2}", a.property1, a.property2, a.property3);      return null; };  var res = list.groupby(func).tolist(); console.writeline(res.count);  mode = groupbyumode.groupby2;  res = list.groupby(func).tolist(); console.writeline(res.count); 

it works witch linq objects shown above. have check if works linq entities or implementation of linq.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -