【问题标题】:Adding two lists having same structure C#添加两个具有相同结构的列表 C#
【发布时间】:2015-03-21 03:56:39
【问题描述】:

所以这是我面临的一个问题 - 我有两个具有以下结构的列表

   public class Payment
      {
        public int Period { get; set; }
        public decimal Balance{ get; set; }
      }

我创建了如下两个列表

 List<Payment> A = new  List<Payment>();
 List<Payment> B = new  List<Payment>();

列表如下所示。

   List A                     List B
Perid  Payment             Perid  Payment
1        10                1         16
2        12                2         13  
3        45                3         44 
4        23                4         33
5        36                5         34
6        45                6         35

我正在尝试从列表 A、B 中添加这两个付款并创建应该具有相同结构的第三个列表。

       List C
 Perid  Payment
  1      10+16
  2      12+13   
  3      45+44  
  4      23+33
  5      36+34
  6      45+35

我理解 for 循环它是可能的,但无论如何,Linq OR Lambda 表达式可以以更简单的方式使用吗? 任何帮助都深表感谢。

【问题讨论】:

  • listA.Concat(listB).GroupBy(x =&gt; x.Period, x =&gt; Balance).Select(x =&gt; new { Key = x.Key, Balance = x.Sum() }).ToList()

标签: c# linq lambda


【解决方案1】:

试试 LINQ 的 Zip 方法。它可以帮助您同时迭代两个集合。

这是一个例子 -

using System;
using System.Linq;

class Program
{
    static void Main()
    {
       // Two source arrays.
       var array1 = new int[] { 1, 2, 3, 4, 5 };
       var array2 = new int[] { 6, 7, 8, 9, 10 };

       // Add elements at each position together.
       var zip = array1.Zip(array2, (a, b) => (a + b));

       // Look at results.
       foreach (var value in zip)
       {
           Console.WriteLine(value);
       }
    }
}

【讨论】:

  • 在你的答案中添加一个例子会让它对其他人更有用。
【解决方案2】:

我认为你不应该这样做。几乎所有阅读代码的人都可以用老式的方式编写代码。

更重要的是,非 LINQ 代码将允许您以合理的方式添加完整性检查(例如,您确定第一个列表中的所有句点都存在于第二个列表中吗?反之亦然?)。

如果你想变得更现代,我建议使用生成器,如下所示:

IEnumerable<Payment> UnitePayments(List<Payment> list1, List<Payment> list2)
{
    ... Check that list1 and list2 are the same length ...
    for(int i=0; i<list1.Length; i++)
    {
         if(list1.Period!=list2.Period) ... handle this case...
         yield return new Payment { Period = list1.Period, 
                                    Balance = list1.Balance + list2.Balance };
    }
}

您的代码读者会感谢您的。

【讨论】:

  • 我不同意。 Linq 及其非常易于识别的方法比任何自定义循环更具可读性,每个开发人员都略有不同。 Linq 让您经常编码,以便您描述您想要的内容,而不是您希望如何完成。
【解决方案3】:

您有两个已经建议的选项:-

使用Concat + GroupBy :-

List<Payment> result = A.Concat(B).GroupBy(x => x.Period)
                                    .Select(x => new Payment
                                    {
                                        Period = x.Key,
                                        Balance = x.Sum(z => z.Balance)
                                    }).ToList();

使用Zip :-

List<Payment> result1 = A.Zip(B, (first, second) => new Payment
                                              {
                                                 Period = first.Period,
                                                 Balance = first.Balance + second.Balance
                                              }).ToList();

您可以参考这个Fiddle。

【讨论】:

    【解决方案4】:

    // 尝试 for 循环,我认为这是处理这种情况的好方法,还有其他 LINQ 查询,但我相信这更容易..

      List<int> a = new List<int>();
        a.Add(1 ) ;
        a.Add(2);
        List<int> b = new List<int>();
        b.Add(5) ;
        b.Add(6);
        List<int> c = new List<int>();
        for (int x = 0; x < a.Count; x++)
        {
            c.Add(a[x] + b[x]);
            Label1.Text += c[x] + "";
        }
    

    【讨论】:

    • 感谢你们的出色投入。看来我只能使用 for 循环。主要问题是,在我的情况下,要添加的列表数量不固定。所以我希望通过使用 Linq 来减少我的 for 循环,但我也意识到它会使事情复杂化。
    猜你喜欢
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    相关资源
    最近更新 更多