【问题标题】:add two strings to two dimensional list将两个字符串添加到二维列表
【发布时间】:2019-06-15 17:14:55
【问题描述】:

我想将两组字符串添加到一个列表中

我收到无法使用添加的错误

        List<List<String>> lastmodified1 = new List<List<String>>();
        lastmodified1.Add(new List<String>());

        foreach (string filenamelocal in files)
        {
            string name = Path.GetFileName(filenamelocal);
            lastmodified1[0][1].Add(Convert.ToString(filenamelocal));
            lastmodified1[0][0].Add(Convert.ToString(File.GetLastAccessTime(filenamelocal)));

        }

【问题讨论】:

  • lastmodified1[0][1] 不是一个列表。这是一个System.String。这就是您收到错误消息的原因。
  • 当您说lastmodified1[0][0] 时,您正在访问第二维中第一个列表的第一个字符串索引

标签: c#


【解决方案1】:

您以第一个索引的列表为目标

        lastmodified1[0].Add(Convert.ToString(filenamelocal));
        lastmodified1[0].Add(Convert.ToString(File.GetLastAccessTime(filenamelocal)));

使用索引 [0] 您是列表的目标,因为第一个列表是隐式的。 所以用

    lastmodified1[0].Add(Convert.ToString(filenamelocal)); //this is accessing to the List inside the First list

您正在从第一个列表访问第二个列表。 使用第二个索引访问第二个列表的值,在本例中为 String,并且您可以获取方法 add,因为字符串没有

    lastmodified1[0][0] //this access to string value

【讨论】:

  • 嗨,如果我希望列表分两列,我该怎么做?
  • 有很多方法可以做到这一点,您可以添加其他列表,或者更好的是,您可以将其设为数组并像列一样访问它,使用.ToArray()@ShayRahamim 或者您对列的意思是什么?跨度>
  • 行列 [0] 列 [1] 类型 [0] "C:\\projects\\holon\\Database16.mdb" "24/04/2019 08:16:38" 字符串 [ 1] "C:\\projects\\holon\\Database5.mdb" "28/04/2019 19:07:40" 字符串 [2] "C:\\projects\\holon\\Database6.mdb" "28 /04/2019 19:07:55" 字符串
  • 你想让它成为数据集??像一个sql表?? @ShayRahamim
  • @ShayRahamim 更多的工作,你需要创建DataTable,并添加你想要使用的列,在你需要在行中插入数据之后,这个链接可能会帮助你stackoverflow.com/questions/37695262/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多