【问题标题】:How to insert numbers from line into array in Collection如何将数字从行插入到集合中的数组中
【发布时间】:2017-01-14 16:22:13
【问题描述】:

我有一个列表,每一行都是这样的:

CAR 1Z2 13 84 ... (200 hundred numbers) ...  ZG2 // splitted by space
.
. 
.
Repeated lines (around 1000 lines with different numbers)

和我的收藏:

public class Test 
{
public string A { set; get; } // first value of line
public string B { set; get; } // second value of line
public double[] C { set; get; } // values[2-200]
public string A { set; get; } // last value of line
}

这是我的部分解决方案:

List<Test> test = new List<Test>();

foreach (string s in list)
        {
            var values = s.Split(null);

            test.Add(new Test
            {
                A = values[0],
                B = values[1],
                C = double.Parse(values[2-200]), // ??
                D = values[201],
            });
        }

如何将值 [2-200] 插入到 Collection 中的 C 数组中?

【问题讨论】:

    标签: c# arrays collections


    【解决方案1】:

    使用 Linq,跳过前 2 个值并获取接下来的 199 个值,将解析转换应用于此序列并具体化数组。

    C = values.Skip(2).Take(199).Select(x => double.Parse(x)).ToArray();
    

    【讨论】:

      猜你喜欢
      • 2016-09-17
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 2014-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多