c# - Combining fields from list of objects into a CSV -



c# - Combining fields from list of objects into a CSV -

linq newbie question.

i combine fields comma separated list when others same in list of objects. not sure if set question correctly.

class { int id; int roll; string name; public a(int id, int roll, string name) { this.id = id; this.roll = roll; this.name = name; } } class programme { static void main(string[] args) { list<a> alist = new list<a>(); a1 = new a(24, 501, "alex"); a2 = new a(12, 27, "steven"); a3 = new a(24, 67, "bob"); a4 = new a(1, 90, "erin"); a5 = new a(12, 27, "christy"); alist.add(a1); alist.add(a2); alist.add(a3); alist.add(a4); alist.add(a5); }

since id , roll of a2 , a5 same (12, 27), when creating new list of objects, have 1 of fields (12, 27, "steven,christy") , rest copied since there wasn't match.

i sorry if question/explanation confusion.

var res = alist .groupby(z => new { z.id, z.roll }) .select(z => new a(z.key.id, z.key.roll, string.join(",", z.select(z2 => z2.name)))) .tolist();

do note id, roll , name have public.

c# linq

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -