【问题标题】:Working with List<List<string>> in c#在 C# 中使用 List<List<string>>
【发布时间】:2017-02-05 19:08:40
【问题描述】:

我是 C# 新手,但我并不完全了解列表的工作原理。这是我的场景...

我有一个List&lt;List&lt;string&gt;&gt;,看起来像这样,{ {x, y, value} }。

示例:{ {1, 10, Red}, {6, 34, Black}, {2, 19, Yellow} }

我需要知道的是如何做到这一点,

if(x == 6 &amp;&amp; y == 34) 然后 value = Black(在本例中)。

List&lt;List&lt;string&gt;&gt; 中有 100 个项目。如何搜索整个内容,查看前两个字符串(x 和 y)的值,然后检索第三个值(“value”)?

【问题讨论】:

  • 而不是列表列表,为什么不只是一个对象列表?无论如何,{1, 10, Red} 似乎并不是一个列表。它看起来更像一个物体

标签: c# list multidimensional-array


【解决方案1】:

我是 C# 新手,我不完全了解列表的工作原理。

List&lt;T&gt; 在底层实现为数组。所以当你写这样的东西时:

var names = new List<string>() {"George", "Jerry", "Cramer", "Elaine" };

您刚刚创建了一个列表,其中包含四个 string 类型。如果你要这样做:

// it will return George because it is accessing item at index 0
var name = names[0]; 

如果你这样做:

var anotherList = new List<List<string>>();
anotherList.Add(names);

您正在创建一个列表,该列表在每个索引处都有另一个列表。所以如果你这样做:

// It will return a list because each index has a list in it.
var item = anotherList[0];

在你的情况下,你应该做的是创建一个类,这会让事情变得更容易:

public class XyValueClass // or a better name
{
    // Change to string if it is not integer
    public int X { get; set; }

    // Change to string if it is not integer
    public int Y { get; set; }

    public string Color { get; set; }
}

那么你可以这样做:

var xyValues = new List<XyValueClass>();
xyValues.Add(new XyValueClass { X = 1, Y = 10, Color = "Red" });

那么当你在搜索的时候,你可以这样做:

// this will return Red
var color = xyValues.Single(item => item.X == 1 && Y == 10).Color; 

如果您知道该项目在那里并且只有一个符合该条件的项目,请使用Single。如果您知道可能只有一项或可能没有,请使用SingleOrDefault。如果您认为有 0 个或多个符合该条件的项目,请使用 Where。

【讨论】:

    【解决方案2】:
    List<List<string>> lsts = new List<List<string>>
    {
        new List<string>{"1", "10", "Red"}, 
        new List<string>{"6", "34", "Black"},
        new List<string>{"2", "19", "Yellow"} 
    };
    
    var values = lsts.Where(l => l[0] == "6" && l[1] == "34").Select(l => l[2]); // "Black"
    

    【讨论】:

      【解决方案3】:

      这个怎么样?直接使用您的 List&lt;List&lt;string&gt;&gt; 结构:

      public static void TestLists()
      {
          List<List<string>> lists = new List<List<string>> { new List<string>{"1", "10", "Red"},new List<string>{"6", "34", "Black"},new List<string>{"2", "19", "Yellow"}};
          foreach (var aList in lists)
          {
              var p = new ListParser(aList);
              if (p.IsValid)
              {
                  if (p.x == 6 && p.y == 34)
                  {
                      Console.WriteLine($"Found desired list: x={p.x}, y={p.y}, Value={p.value}");
                  }
                  else
                  {
                      Console.WriteLine($"Found another list: x={p.x}, y={p.y}, Value={p.value}");
                  }
              }
          }
          Console.ReadLine();
      }
      

      通过使用assisting 类ListParser:

      public class ListParser
      {
          public int x;
          public int y;
          public string value;
          public bool IsValid = false;
          public ListParser(List<string> row)
          {
              if (row != null && row.Count >= 3 && int.TryParse(row[0], out x) && int.TryParse(row[1], out y))
              {
                  IsValid = true;
                  value = row[2];
              }
          }
      }
      

      这是输出:

      【讨论】:

        【解决方案4】:

        这段代码闻起来很糟糕,但应该是这样的:

        var outerList = new List<List<string>>()
        {
            new List<string>() { x="1", y="10", c=(string)Colors.Red },
            new List<string>() { ... },
            new List<string>() { ... },
            ....etc....
        };
        
        outerList.Select(inner => inner.Where(a => a.x == 6 && a.y == 34).Select(a => a.c));
        

        【讨论】:

          【解决方案5】:

          作为另一个(可能更灵活)选项,定义一个新类:

          public class Item
          {
              public int x;
              public int y;
              public Color value;
          }
          

          现在您可以拥有一个 Item 列表并使用 Linq。例如:

          List<Item> mylist;
          var s = mylist.FirstOrDefault(a => a.x==6 && a.y == 36).value;
          

          【讨论】:

            猜你喜欢
            • 2016-09-24
            • 2012-12-26
            • 2020-10-28
            • 2013-11-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-03-18
            • 1970-01-01
            相关资源
            最近更新 更多