我有一个这样的课程:
class item
{
public int Id { get; set; }
public Dictionary<string, int> Data { get; set; }
}
我有2个列表包含这样的类对象:
item obj1 = new item();
obj1.Id = 1;
obj1.Data = new Dictionary<string, int>();
obj1.Data.Add("1", 11);
obj1.Data.Add("2", 12);
obj1.Data.Add("3", 13);
item obj2 = new item();
obj2.Id = 2;
obj2.Data = new Dictionary<string, int>();
obj2.Data.Add("1", 21);
obj2.Data.Add("2", 22);
obj2.Data.Add("3", 23);
item obj3 = new item();
obj3.Id = 3;
obj3.Data = new Dictionary<string, int>();
obj3.Data.Add("1", 31);
obj3.Data.Add("2", 32);
obj3.Data.Add("3", 33);
item obj4 = new item();
obj4.Id = 1;
obj4.Data = new Dictionary<string, int>();
obj4.Data.Add("1", 41);
obj4.Data.Add("2", 42);
obj4.Data.Add("3", 43);
item obj5 = new item();
obj5.Id = 2;
obj5.Data = new Dictionary<string, int>();
obj5.Data.Add("1", 51);
obj5.Data.Add("2", 52);
obj5.Data.Add("3", 53);
item obj6 = new item();
obj6.Id = 3;
obj6.Data = new Dictionary<string, int>();
obj6.Data.Add("1", 61);
obj6.Data.Add("2", 62);
obj6.Data.Add("3", 63);
List<item> lst1 = new List<item>();
lst1.Add(obj1);
lst1.Add(obj2);
lst1.Add(obj3);
List<item> lst2 = new List<item>();
lst2.Add(obj4);
lst2.Add(obj5);
lst2.Add(obj6);
现在我想根据对象的id和字典键添加字典的值。结果将是这样的:
List<item> result = new List<item>();
result[0].Id = 1
result[0].Data["1"] = 52 // obj1.Data[1] + obj4.Data[1]
result[0].Data["2"] = 54 // obj1.Data[2] + obj4.Data[2]
result[0].Data["3"] = 56 // obj1.Data[3] + obj4.Data[3]
result[1].Id = 2
result[1].Data["1"] = 72 // obj2.Data[1] + obj5.Data[1]
result[1].Data["2"] = 74 // obj2.Data[2] + obj5.Data[2]
result[1].Data["3"] = 76 // obj2.Data[3] + obj5.Data[3]
result[2].Id = 3
result[2].Data["1"] = 92 // obj3.Data[1] + obj6.Data[1]
result[2].Data["2"] = 94 // obj3.Data[2] + obj6.Data[2]
result[2].Data["3"] = 96 // obj3.Data[3] + obj6.Data[3]
获得此结果的最佳方式是什么?
谢谢
答案 0 :(得分:2)
首先,您作为示例显示的输出是错误的。例如,obj3.Data[1] + obj6.Data[1]
等于 92 ,而不是56。
所以,这里有 LINQ 的所有美丽:
var result = lst1.Join(lst2, x => x.Id, y => y.Id, (x, y) => new item
{
Id = x.Id,
Data = x.Data.Join(y.Data,
a => a.Key,
b => b.Key,
(a, b) => new { a.Key, Value = a.Value + b.Value }).ToDictionary(r => r.Key, t => t.Value)
}).ToList();
如您所愿,结果是:
result[0].Id = 1
result[0].Data["1"] = 52 // obj1.Data[1] + obj4.Data[1]
result[0].Data["2"] = 54 // obj1.Data[2] + obj4.Data[2]
result[0].Data["3"] = 56 // obj1.Data[3] + obj4.Data[3]
result[1].Id = 2
result[1].Data["1"] = 72 // obj2.Data[1] + obj5.Data[1]
result[1].Data["2"] = 74 // obj2.Data[2] + obj5.Data[2]
result[1].Data["3"] = 76 // obj2.Data[3] + obj5.Data[3]
result[2].Id = 3
result[2].Data["1"] = 92 // obj3.Data[1] + obj6.Data[1]
result[2].Data["2"] = 94 // obj3.Data[2] + obj6.Data[2]
result[2].Data["3"] = 96 // obj3.Data[3] + obj6.Data[3]
答案 1 :(得分:2)
这是一个更长的方法,但它很容易让你能够用一个函数添加多个项目列表列表,并且根据你的Linq掌握可能更容易理解/调试:
private static List<Item> AddItemLists(List<List<Item>> itemLists)
{
if (itemLists == null) throw new ArgumentNullException("itemLists");
var result = new List<Item>();
foreach (var itemList in itemLists)
{
foreach (var item in itemList)
{
var existingItemWithId = result.FirstOrDefault(i => i.Id == item.Id);
if (existingItemWithId == null)
{
result.Add(item);
}
else
{
foreach (var itemData in item.Data)
{
if (existingItemWithId.Data.ContainsKey(itemData.Key))
{
existingItemWithId.Data[itemData.Key] += itemData.Value;
}
else
{
existingItemWithId.Data.Add(itemData.Key, itemData.Value);
}
}
}
}
}
return result;
}
使用示例:
var obj1 = new Item {Id = 1, Data =
new Dictionary<string, int> {{"1", 11}, {"2", 12}, {"3", 13}}};
var obj2 = new Item {Id = 2, Data =
new Dictionary<string, int> {{"1", 21}, {"2", 22}, {"3", 23}}};
var obj3 = new Item {Id = 3, Data =
new Dictionary<string, int> {{"1", 31}, {"2", 32}, {"3", 33}}};
var obj4 = new Item {Id = 1, Data =
new Dictionary<string, int> {{"1", 41}, {"2", 42}, {"3", 43}}};
var obj5 = new Item {Id = 2, Data =
new Dictionary<string, int> {{"1", 51}, {"2", 52}, {"3", 53}}};
var obj6 = new Item {Id = 3, Data =
new Dictionary<string, int> {{"1", 61}, {"2", 62}, {"3", 63}}};
// Three lists of items that we want to 'add'
var lst1 = new List<Item> {obj1, obj2};
var lst2 = new List<Item> {obj3, obj4};
var lst3 = new List<Item> {obj5, obj6};
// Add up as many lists of items that you want
var result = AddItemLists(new List<List<Item>> {lst1, lst2, lst3});
结果如下: