This question is a slightly modified version of Convert List<T> to Dictionary with strategy
I have List < DTO > where DTO class looks like this,
private class DTO
{
public string Name { get; set; }
public int Count { get; set; }
}
I create objects and add it to List.
var dto1 = new DTO { Name = "test", Count = 2 };
var dto2 = new DTO { Name = "test", Count = 3 };
var dtoCollection = new List<DTO> {dto1, dto2};
Now my requirement is I need to create a List from the dtoCollection where Name field should be unique across the entire List.
For example, if you convert the above dtoCollection to the required List from, the resultant list should be like:
List < DTO > count should be 1;
The object inside the list should be a single DTO with Name as “test” and Count as 5
Where Count is obtained from summing up the Count fields in all DTO’s whose Name fields are same
1 Answer