Mapping .NET objects, grouping objects

Suppose I have a list of such objects:

public class FlatModel { public string groupName { get; set; } public decimal value1 { get; set; } public decimal value2 { get; set; } public decimal value3 { get; set; } } 

and I want to map them to the next object, grouping them by GroupName

 public class GroupedModel { public string groupName { get; set; } public List<ModelValues> values { get; set; } } public class ModelValues { public decimal value1 { get; set; } public decimal value2 { get; set; } public decimal value3 { get; set; } } 

Is there a direct way to do this with Automapper, Value Injector, or some other object mapping utility?

+4
source share
1 answer

Can it work for you?

  var arr = new List<FlatModel>(); var result = from p in arr group p by p.groupName into g select new GroupedModel { groupName = g.Key, values = (from q in g select new ModelValues { value1 = q.value1, value2 = q.value2, value3 = q.value3 }).ToList() }; 
+1
source

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


All Articles