简体   繁体   English

在多维数组中查找值并返回所有项目c#

[英]Find Value in Multidimensional Array and return all items c#

here is the issue, I have an array defined like below: 这是问题,我有一个如下定义的数组:

int[,] Users = new int[1000,3];

Its data will be something like: 它的数据类似于:

  • 0,1,2 0,1,2
  • 1,2,1 1,2,1
  • 2,3,2 2,3,2
  • 3,3,4 3,3,4
  • 4,2,3 ... 4,2,3 ......

the array is used as needed by my script. 我的脚本根据需要使用该数组。 but I need to be able to filter the array based on one of its dimensions, and return all available matches. 但我需要能够根据其中一个维度过滤数组,并返回所有可用的匹配项。

For example, filtering on dimension [1], wanting all that match '3' will return an array containing: 例如,对维度[1]进行过滤,希望所有匹配的“3”将返回一个包含以下内容的数组:

  • 2,3,2 2,3,2
  • 3,3,4 3,3,4

Could anyone give me a hand with this? 有人能帮我一把吗?

Many thanks. 非常感谢。

If you can change your array from int[,] to int[][] then you can easily achieve this using LINQ. 如果你可以将数组从int[,]更改为int[][]那么你可以使用LINQ轻松实现这一点。

  int[][] users = new int[][]
  {
    new int[]{0,1,2},
    new int[]{1,2,1},
    new int[]{2,3,2},
    new int[]{3,3,4},
    new int[]{4,2,3}
  };


  var result = from u in users 
               where u[1] == 3 
               select u;

If changing your array is not an option then you could write a Filter function as follows. 如果不能更改数组,那么可以按如下方式编写Filter函数。

public static IEnumerable<T[]> Filter<T>(T[,] source, Func<T[], bool> predicate)
{
  for (int i = 0; i < source.GetLength(0); ++i)
  {
    T[] values = new T[source.GetLength(1)];
    for (int j = 0; j < values.Length; ++j)
    {
      values[j] = source[i, j];
    }
    if (predicate(values))
    {
      yield return values;
    }
  }      
}

The above could then be called as follows 然后可以如下调用上述内容

var result = Filter(users, u => u[1] == 3);

You could take this a step further and implement your own custom Linq extension for the Where function which would allow you to filter the T[,] arrays. 您可以更进一步,为Where函数实现自己的自定义Linq扩展,这将允许您过滤T[,]数组。 Here is a naive example that could get you started. 这是一个可以帮助你入门的天真例子。

public static class LinqExtensions
{
  public static IEnumerable<T[]> Where<T>(this T[,] source, Func<T[], bool> predicate)
  {
    if (source == null) throw new ArgumentNullException("source");
    if (predicate == null) throw new ArgumentNullException("predicate");
    return WhereImpl(source, predicate);
  }

  private static IEnumerable<T[]> WhereImpl<T>(this T[,] source, Func<T[], bool> predicate)
  {      
    for (int i = 0; i < source.GetLength(0); ++i)
    {
      T[] values = new T[source.GetLength(1)];
      for (int j = 0; j < values.Length; ++j)
      {
        values[j] = source[i, j];
      }
      if (predicate(values))
      {
        yield return values;
      } 
    }
  }    
}

With this you can again use Linq as in the first example to filter the array 有了这个,您可以再次使用Linq,如第一个示例中所示,以过滤数组

  var result = from u in users 
               where u[1] == 3 
               select u;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM