c# - Group by linq for nested objects -


i making group linq statement convert single list of data list nested list. here code far:

[testmethod] public void linqtestnestedselect2() {     // initialization      list<combi> listtolinq = new list<combi>() {          new combi{ id = 1, desc = "a", name = "a", count = 1 },         new combi{ id = 1, desc = "b", name = "a", count = 2 },         new combi{ id = 2, desc = "c", name = "b", count = 3 },         new combi{id = 2,  desc = "d", name = "b", count = 4 },     };      // linq group     var result = (from row in listtolinq                   group new { des = row.desc, count = row.count } new { name = row.name, id = row.id } obj                   select new { name = obj.key.name, id = obj.key.id, descriptions = (from r in obj select new b() { des = r.des, count = r.count }).tolist() }).tolist();      // validation of results     assert.areequal(2, result.count);     assert.areequal(2, result[0].descriptions.count);     assert.areequal(2, result[0].descriptions.count);     assert.areequal(2, result[1].descriptions.count);     assert.areequal(2, result[1].descriptions.count); }  public class {     public int id;     public string name;      public list<b> descriptions; }  public class b {     public int count;     public string des; }  public class combi {     public int id;     public string name;     public int count;     public string desc; } 

this fine if objects small example. implement objects lot more properties. how can efficiently write statement don't have write field names twice in linq statement?

i return objects in statement , want like:

// not working wishfull thinking code  var result = (from row in listtolinq               group new { des = row.desc, count = row.count } new { name = row.name, id = row.id } obj               select new (a){ = obj.key , descriptions = obj.tolist<b>()}).tolist(); 

background: re writing web api retrieves objects nested objects in single database call sake of db performance. it's big query join retrieves crap load of data need sort out objects.
probably important: id unique.

edit: based on answers far have made solution sort of works me, still bit ugly, , want better looking.

{     // start part     return (from row in reader.asenumerable()             group row row.id grouping             select createa(grouping)).tolist(); }  private static createa(igrouping<object, listtolinq> grouping) {      retval = staticcreateafunction(grouping.first());     retval.descriptions = grouping.select(item => staticcreatebfunction(item)).tolist();     return ret; } 

i hope staticcreateafunction obvious enough does. in scenario have write out each property once, wanted. hope there more clever or linq-ish way write this.

var result = (from row in listtolinq               group new b { des = row.desc, count = row.count } new { name = row.name, id = row.id } obj               select new { name = obj.key.name, id = obj.key.id, descriptions = obj.tolist() }).tolist(); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -