【发布时间】:2019-05-17 06:13:38
【问题描述】:
我正在使用 C#。
如何在两个列表中组合(求和、加、减)这些类元素?
class Attribute
{
public AttributeType WhatAttri;
public float amount;
}
enum AttributeType{
maxhp, str, dex, int, wis,,,,
}
Attribute[] attList1;
Attribute[] attList2;
如果具体数值是这样的,
attList1[0] = new Attribute(AttributeType.maxhp, 6)
attList1[1] = new Attribute(AttributeType.str, 4)
attList1[2] = new Attribute(AttributeType.dex, 3)
attList2[0] = new Attribute(AttributeType.str, 9)
attList2[1] = new Attribute(AttributeType.int, 7)
attList2[2] = new Attribute(AttributeType.wis, 5)
我想要这样的最终结果,(添加attList1 值,减去attList2 值,以及求和(或减或加)重复的AttributeType)
所以在上面两个列表中,AttributeType.str 是相同的,所以从attList1[1]的值(4)中减去重复的attList2[0]的数量变量的值(9) 并从 attList2 中排除该元素。
所以最终的结果应该是,
Attribute[] combinedList; (or List<Attribute> combinedList )
combinedList[0] = new Attribute(AttributeType.maxhp, 6)
combinedList[1] = new Attribute(AttributeType.str, -5) (4 - 9)
combinedList[2] = new Attribute(AttributeType.dex, 3)
combinedList[3] = new Attribute(AttributeType.int, -7)
combinedList[4] = new Attribute(AttributeType.wis, -5)
如何做到这一点?
谢谢。
【问题讨论】:
-
到目前为止你尝试了什么?
-
分组,总和?有人上当了。
-
我不知道。只是在谷歌上搜索 concat、join、zip,但还不知道。
-
取Lsource,添加LUpdateState。您现在将拥有一个带有“重复”AttributeType 的列表。 GroupBy 在 AttributeType 上。使用 AttributeType = group.key 选择新属性。和值 = sum(group.amount)