【问题标题】:Transform Two Lists into One Different List将两个列表转换为一个不同的列表
【发布时间】:2010-04-13 15:29:05
【问题描述】:

我有两个double 列表

List<double> X
List<double> Y

我有一个目标对象:

public class PointD 
{
  public double X
  {get;set;}
  public double Y
  {get;set;}
}

如何将它们转换为单个列表?

public static List<PointD> Transform(List<double> X, List<double> Y)
{
}

所有错误检查都必须在那里。

答案必须在 LINQ 中,抱歉之前没有指定!

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    升级到 .NET 4 并使用 Enumerable.Zip?

    【讨论】:

    【解决方案2】:

    这样的东西可以满足你的需要

    Enumerable.Range(0, X.Count).Select(i=>new PointD{X = X[i], Y = Y[i]).ToList()
    

    假设 X.Count == Y.Count。否则,需要做一些检查

    【讨论】:

      【解决方案3】:
      public static List<PointD> Transform(List<double> x, List<double> y)
      {
          if (x == null)
              throw new ArgumentNullException("x", "List cannot be null!");
      
          if (y == null)
              throw new ArgumentNullException("y", "List cannot be null!");
      
          if (x.Count != y.Count)
              throw new ArgumentException("Lists cannot have different lengths!");
      
          List<PointD> zipped = new List<PointD>(x.Count);
      
          for (int i = 0; i < x.Count; i++)
          {
               zipped.Add(new PointD { X = x[i], Y = y[i] });
          }
      
          return zipped;
      }
      

      【讨论】:

      • x 和 y 没有 Length 属性。改为使用计数
      • @Daniel,@CAbbot:糟糕,已修复!
      【解决方案4】:
          public static List<PointD> Transform(List<double> X, List<double> Y)
          {
              if (X == null || X.Count == 0)
              {
                  throw new ArgumentException("X must not be empty");
              }
              if (Y == null || Y.Count == 0)
              {
                  throw new ArgumentException("Y must not be empty");
              }
              if (X.Count != Y.Count)
              {
                  throw new ArgumentException("X and Y must be of equal length");
              }
              var results = new List<PointD>();
              for (int i = 0; i < X.Count; i++)
              {
                  results.Add(new PointD { X = X[i], Y = Y[i]});
              }
              return results;
          }
      

      【讨论】:

        【解决方案5】:

        这是另一个解决方案(使用 .NET 3.5)。将其包装在具有所需错误处理的方法中。假设XList.Count &lt;= YList.Count

        var points = XList.Select((i, j) => new PointD { X = i, Y = YList[j] });
        

        【讨论】:

          【解决方案6】:

          这是另一个使用两个双精度列表的 3.5 方法。

          var joined = from item1 in list1.Select((d, index) => new { D = d, Index = index })
                       join item2 in list2.Select((d, index) => new { D = d, Index = index })
                       on item1.Index equals item2.Index
                       select new { X = item1.D, Y = item2.D };
          

          【讨论】:

            猜你喜欢
            • 2016-05-27
            • 2020-06-08
            • 1970-01-01
            • 1970-01-01
            • 2012-09-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多