I'm trying to convert an object to a dto
public class topology
{
public int a { get; set; }
public int b { get; set; }
}
to
public class topologyDto
{
public int a { get; set; }
public List<int> b { get; set; }
}
What I have right now that is mapping doesn't convert it to a list:
public IEnumerable<topologyDto> GetTopology()
{
return _dataProvider.GetTopology()
.Select(x => new topologyDto
{
a= x.a,
b= x.b
};
}
A test set would look something like this, where I want to map to the topologyDto:
var data = new []
{
new topology() { a = 1, b = 1 },
new topology() { a = 1, b = 2 },
new topology() { a = 1, b = 3 },
new topology() { a = 1, b = 4 },
new topology() { a = 2, b = 1 },
new topology() { a = 2, b = 2 },
new topology() { a = 2, b = 3 },
new topology() { a = 2, b = 4 },
};
var test = new []
{
new topologyDto() { a = 1, b = new List<int>() { 1, 2, 3, 4 }, },
new topologyDto() { a = 2, b = new List<int>() { 1, 2, 3, 4 }, },
}
CodePudding user response:
Looks like you want to group by a:
_dataProvider.GetTopology()
.GroupBy(x => x.a)
.Select(g => new topologyDto
{
a = g.Key,
b = g.Select(t => t.b).ToList(),
} );
