如何合并列表元素? (C#)

时间:2019-05-17 06:13:39

标签: c# arrays list

我正在使用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)

如何实现?

谢谢。

3 个答案:

答案 0 :(得分:1)

var result =
     attList2.Select(a => new Attribute(a.WhatAttri, -a.amount)) // line 1
     .Concat(attList1)  // line 2
     .GroupBy(a => a.WhatAttri)  // line 3
     .Select(g => new Attribute(g.Key, g.Sum(a => a.amount)));  // line4

  foreach(var a in result)
  {
    Console.WriteLine($"{a.WhatAttri}: {a.amount}");
  }

您要对第一个列表的计数求和,然后减去第二个列表的计数。因此,首先,我将第二个列表转换为带有负数的新列表(第1行)。然后将两个列表合并为一个列表(第2行)。 然后将大行按类型分组(第3行)。然后,您将拥有一个键和项的结构,在其中您可以使用键和金额的总和来创建新的属性(第4行)。

编辑:将第2行中的“联盟”替换为“ Concat”,以避免在属性类中存在自定义比较器方法的情况下删除重复的值

答案 1 :(得分:0)

主要问题是,属性修改器中没有正值或负值。您将如何提升这些属性?修复后,解决方案很容易

添加两个带有concat,GroupBy AttributeType的列表,然后选择值。

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {            
        var attributes = new Attribute[] {
            new Attribute{WhatAttri=AttributeType.maxhp, amount=6 },
            new Attribute{WhatAttri=AttributeType.str, amount=4 },
            new Attribute{WhatAttri=AttributeType.dex, amount=3 },
        };

        //Attribute modifier has to be either positive or negative
        var attributesModifier = new Attribute[] {
            new Attribute{WhatAttri=AttributeType.str, amount=-9 },
            new Attribute{WhatAttri=AttributeType.@int, amount=-7 },
            new Attribute{WhatAttri=AttributeType.wis, amount=-5 },
        };


        var newAttributes = attributes
                                .Concat(attributesModifier)
                                .GroupBy(x => x.WhatAttri)
                                .Select(group => 
                                        new Attribute { 
                                            WhatAttri = group.Key, 
                                            amount = group.Sum(g => g.amount) 
                                        });


        newAttributes.Dump();
    }

    public class Attribute
    {
        public AttributeType WhatAttri { get; set; }
        public float amount { get; set; }
    }

    public enum AttributeType
    {
        maxhp, str, dex, @int, wis
    }
}

在线演示https://dotnetfiddle.net/3C7n7F

答案 2 :(得分:0)

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public enum AttributeType
{
    maxhp, str, dex, intel, wis,
}

[System.Serializable]
public class Attribute
{
    public AttributeType WhatAttri;
    public float amount;
    public Attribute(AttributeType type, int a)
    {
        WhatAttri = type;
        amount = a;
    }
}

public class LinqTest : MonoBehaviour
{
    Attribute[] attList1 = new Attribute[3];
    Attribute[] attList2 = new Attribute[3]; 
    void Start()
    {
        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.intel, 7);
        attList2[2] = new Attribute(AttributeType.wis, 5);

        Calcul();
    }

    void Calcul()
    {
        var result = attList2
                       .Select(a => new Attribute(a.WhatAttri, -(int)a.amount)) // line 1
                       .Union(attList1)  // line 2
                       .GroupBy(a => a.WhatAttri)  // line 3
                       .Select(g => new Attribute(g.Key, g.Sum(a => (int)a.amount)));  // line4

        foreach (var a in result)
        {
            Debug.Log($"{a.WhatAttri}: {a.amount}");
        }
    } 
}

这是通过答案测试以上代码的最终结果。 使用Unity引擎。