So I have this class structure:
public class ObjectCode
{
public string Code { get; set; }
}
class Configuration
{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<ObjectCode> Objects { get; set; }
}
Configuration
ObjectCode
Configuration
Use SelectMany
inside each group in GroupBy
:
var res = congigs
.GroupBy(c => new {c.Id, c.Name})
.Select(g =>
new Configuration {
Id = g.Key.Id
, Name = g.Key.Name
, Objects = g
.SelectMany(c => c.Objects)
.GroupBy(c => c.Code)
.Select(gg => gg.First())
}
).ToList();
The above assumes that Config
objects with the same Id
also have identical Name
s.