列表集合转换

时间:2014-10-04 17:26:57

标签: c# collections

请给我一些建议。如何直接从List [string []]转换为List [int []]或转换为我的define对象,例如FreightInTeritory

这里是转换代码List to List:

        List<string[]> obj = new List<string[]>();
        obj.Add(new string[]{"1", "2", "3", "4"});
        obj.Add(new string[] { "1", "2", "3", "4" });
        obj.Add(new string[] { "1", "2", "3", "4" });
        obj.Add(new string[] { "1", "2", "3", "4" });

        var l1 = obj.ConvertAll(s=>Int32.Parse(s.ToString()));

以及如何从类对象(FreightInTeritory)转换为例如List [string []]非常感谢.....

   public class FreightInTeritory
   {
    private string territoryDescription;
    private double freight;

    public string TerritoryDescription { get; set; }
    public string Freight {get; set;}
   }

1 个答案:

答案 0 :(得分:1)

你为什么要转换?你可以从一开始就列出一个int。

    List<int[]> obj = new List<int[]>();

并添加值

    obj.Add(new int[]{1, 2, 3});

FreightInTeritory对象可以存储在FreightInTeritory列表中

    List<FreightInTeritory> FreightInTeritoryList = new List<FreightInTeritory>();