0

我有两个对象列表集合,格式如下:

list1{
       new Object1{ State= "AL" , Value = 3.123}
       new Object2{ State= "CO", Value = 2.111}
       }

list2{
       new Object1{State="AL", Value=2.123}
       new Object2{State="CO", Value=3.111}
     }

我需要比较这两个列表并生成另一个列表,如下所示:

list3{
      new Object1{State="AL", Value= (3.123 + 2.123)}
      new Object2{ State="CO", Value =(2.111 + 3.111) }
     }

有人可以告诉我我该怎么做吗?

4

3 回答 3

2

看一下这个:

public class MyEntity
{
    public string Name { get; set; }
    public int Value { get; set; }
}

public static class MyEntityListExtension
{
    public static List<MyEntity> AddList(this List<MyEntity> FirstList, List<MyEntity> SecondList)
    {
        List<MyEntity> ReturnList = new List<MyEntity>();

        foreach (MyEntity CurrentEntity in FirstList)
        {
            MyEntity TempEntity = SecondList.Where<MyEntity>(x => x.Name.Equals(CurrentEntity.Name)).SingleOrDefault<MyEntity>();

            if (TempEntity != null)
            {
                ReturnList.Add(new MyEntity()
                {
                    Name = CurrentEntity.Name,
                    Value = CurrentEntity.Value + TempEntity.Value
                });
            }
        }

        return ReturnList;
    }
}

用法:

        List<MyEntity> list1 = new List<MyEntity>();
        List<MyEntity> list2 = new List<MyEntity>();
        List<MyEntity> addedList = new List<MyEntity>();

        list1.Add(new MyEntity()
        {
            Name = "A",
            Value = 1
        });

        list1.Add(new MyEntity()
        {
            Name = "B",
            Value = 1
        });

        list2.Add(new MyEntity()
        {
            Name = "A",
            Value = 2
        });


        addedList = list1.AddList(list2);

问候


好的,我想出了第二个解决方案。看,我不是 lambda 专家,所以我只是觉得这太棒了!

public static class MyEntityListExtension
{
    public static List<MyEntity> AddList(this List<MyEntity> FirstList, List<MyEntity> SecondList)
    {
        return FirstList.Join<MyEntity, MyEntity, string, MyEntity>(SecondList, x => x.Name, y => y.Name, (x, y) =>
            {
                return new MyEntity()
                {
                    Name = x.Name,
                    Value = x.Value + y.Value
                };
            }).ToList<MyEntity>();
    }
}
于 2012-07-31T05:06:12.717 回答
2

安德烈的回答肯定会奏效。您可以将其推广到分组的概念并支持任意数量的IEnumerables合并

List<MyEntity> source1 = ...;
List<MyEntity> source2 = ...;
IEnumerable<MyEntity> source3 = ...;

var mergedList = (from item in source1.Contact(source2).Concat(source3)
                  group item by item.Name into g
                  select new MyEntity { Name = g.Key, Value = g.Sum(e => e.Value) })
                  .ToList();
于 2012-07-31T05:23:35.747 回答
0

我还没有测试过代码,但这可能会给你一些想法......

 List<Obj> list1= New List(obj1,obj2);
 List<Obj> list2= New List(obj3,obj4);

    var allObjects= 
        from list1.Concat(list2); 

    var list3=from o in allObjects
            group o by o.State into g 
        select new {State=g.key,Value=g.Sum(p => p.Value)};

    foreach (var obj in list3) 
    { 
        Console.WriteLine("{0},{1}",obj.State,obj.Value); 
    } 
于 2012-07-31T05:58:53.753 回答