Convert Datatable to Object with Linq and Group by

I am trying to convert datatable to JSON with a special format

Data in a DataTable Follows

col1 col2 col3 col4 --------------------- AB c D1 AB c D2 AB c D3 

Try converting it to an array of objects, e.g.

 class obj { var col1; var col2; var col3; list<string> col4; } 

I'm trying to use linq, but it seems to be stuck.

  var result = from row in dt.AsEnumerable() group row by new { c1 = row["col1"], c2 = row["col2"], c3 = row["col3"] } into section select new { item = section.Key }; 
+4
source share
1 answer
 var result = from row in dt.AsEnumerable() group row by new { c1 = r.Field<string>("col1"), c2 = r.Field<string>("col2"), c3 = r.Field<string>("col3") } into section select new { col1 = section.Key.c1, col2 = section.Key.c2, col3 = section.Key.c2, col4 = section.Select(r => r.Field<string>("col4")).ToList() }; 
+5
source

Source: https://habr.com/ru/post/1442855/


All Articles