【问题标题】:List collection conversion列表集合转换
【发布时间】:2014-10-04 17:26:57
【问题描述】:

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

这是 List 到 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;}
   }

【问题讨论】:

  • 关于您的第一个问题,您几乎已经自己回答了。剩下的就是将l1int[] 转换为List&lt;int&gt;(不是List[int[]] 顺便说一句。)。使用new List&lt;int&gt;(l1)l1.ToList()。 (后一个选项需要 using System.Linq; 指令。)

标签: c# collections


【解决方案1】:

你为什么要转换?你可以从头开始制作一个整数列表。

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

并添加值

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

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

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

【讨论】:

    猜你喜欢
    • 2013-12-16
    • 1970-01-01
    • 2011-01-29
    • 2011-12-25
    • 1970-01-01
    • 2017-02-06
    • 2022-11-12
    • 1970-01-01
    • 2011-08-02
    相关资源
    最近更新 更多