【发布时间】:2012-11-14 12:57:24
【问题描述】:
本题是Convert List<T> to Dictionary with strategy的略微修改版
我有 List
private class DTO
{
public string Name { get; set; }
public int Count { get; set; }
}
我创建对象并将其添加到列表中。
var dto1 = new DTO { Name = "test", Count = 2 };
var dto2 = new DTO { Name = "test", Count = 3 };
var dtoCollection = new List<DTO> {dto1, dto2};
现在我的要求是我需要从 dtoCollection 创建一个 List,其中 Name 字段在整个 List 中应该是唯一的。
例如,如果您将上述 dtoCollection 转换为所需的 List from,则结果列表应如下所示:
列表
列表中的对象应该是单个 DTO,Name 为“test”,Count 为 5
其中 Count 是通过对 Name 字段相同的所有 DTO 中的 Count 字段求和得到的
【问题讨论】: