如何使用LINQ用2维阵列 [英] How to use LINQ with a 2 dimensional array

查看:357
本文介绍了如何使用LINQ用2维阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2维字节数组,看起来是这样的:


  

0 0 0 0 1


  
  

1 1 1 1 0


  
  

0 0 1 1 1


  
  

1 0 1 0 1


阵列中的每个值只能是0或1以上的简化例示出4行,每行具有5个列。我试图找出如何使用LINQ到指数返回到已设定的1的数量最多的行,这在上面的例子应该返回1。

以下非LINQ C#code解决了这个问题:

 静态INT GetMaxIndex(字节[,] TwoDArray)
{
   //该方法找到设置1秒的最大数目的行。
   //
   INT numRows行= TwoDArray.GetLength(0);
   INT NUMCOLS = TwoDArray.GetLength(1);
   INT行数,MaxRowCount = 0,MaxRowIndex = 0;
   //
   对于(INT LoopR = 0; LoopR<其行; LoopR ++)
   {
      行数= 0;
      对于(INT LoopC = 0; LoopC< NUMCOLS; LoopC ++)
      {
         如果(TwoDArray [LoopR,LoopC]!= 0)
            行数++;
      }
      如果(行数> MaxRowCount)
      {
         MaxRowCount =行数;
         MaxRowIndex = LoopR;
      }
   }
   返回MaxRowIndex;
}静态无效的主要()
{
   字节[,] Array2D =新的字节[4,5] {{0,0,0,0,1},{1,1,1,1,0},{0,0,1,1,1}, {1,0,1,0,1}};
   INT MaxInd = GetMaxIndex(Array2D);
   Console.WriteLine(MaxInd = {0},MaxInd);
}

所以,我的问题是:


  1. LINQ如何可以被用来解决这个问题,并会使用LINQ这里是低效率的,使用上述非LINQ code?

  2. 是否有可能解决这个问题,PLINQ?或者说,这将是更有效地使用任务并行库(TPL)直接为超过code和每一行中拆出1的个数的计数到一个单独的线程,假设每个行至少1000列?


解决方案

这是很难与LINQ多维数组工作,但这里是你如何能做到:

  VAR ARR =新[,] {{0,0,0,0,1},{1,1,1,1,0},{0,0,1 ,1,1},{1,0,1,0,1}};VAR数据=
    Enumerable.Range(0,4)
        。选择(
            排=>
                新
                {
                    指数=行,
                    计数= Enumerable.Range(0,5)。选择(COL = GT;改编[行,列])计数(X =&X的催化剂== 1)。
                })
        .OrderByDescending(X => x.count)
        。选择(X => x.index)
        。第一();

I have a 2-dimensional byte array that looks something like this:

0 0 0 0 1

1 1 1 1 0

0 0 1 1 1

1 0 1 0 1

Each value in the array can only be 0 or 1. The above simplified example shows 4 rows with each row having 5 columns. I am trying to figure out how to use LINQ to return the index to the row that has the largest number of 1s set, which in the above example should return 1.

The following non LINQ C# code solves the problem:

static int GetMaxIndex(byte[,] TwoDArray)
{
   // This method finds the row with the greatest number of 1s set.
   //
   int NumRows = TwoDArray.GetLength(0);
   int NumCols = TwoDArray.GetLength(1);
   int RowCount, MaxRowCount = 0, MaxRowIndex = 0;
   //
   for (int LoopR = 0; LoopR < NumRows; LoopR++)
   {
      RowCount = 0;
      for (int LoopC = 0; LoopC < NumCols; LoopC++)
      {
         if (TwoDArray[LoopR, LoopC] != 0)
            RowCount++;
      }
      if (RowCount > MaxRowCount)
      {
         MaxRowCount = RowCount;
         MaxRowIndex = LoopR;
      }
   }
   return MaxRowIndex;
}

static void Main()
{
   byte[,] Array2D = new byte[4, 5] { { 0, 0, 0, 0, 1 }, { 1, 1, 1, 1, 0 }, { 0, 0, 1, 1, 1 }, { 1, 0, 1, 0, 1 } };
   int MaxInd = GetMaxIndex(Array2D);
   Console.WriteLine("MaxInd = {0}", MaxInd);
}

So, my questions are:

  1. How can LINQ be used to solve this, and would using LINQ here be less efficient that using the non LINQ code above?
  2. Is it possible to solve this problem with PLINQ? Or, would it be more efficient to use the Task Parallel Library (TPL) directly for the above code and split out the count of the number of 1s in each row to a separate thread, assuming that each row has at least 1,000 columns?

解决方案

It's hard to work with multidimentional arrays with LINQ, but here's how you could do:

var arr = new [,] { { 0, 0, 0, 0, 1 }, { 1, 1, 1, 1, 0 }, { 0, 0, 1, 1, 1 }, { 1, 0, 1, 0, 1 } };

var data =
    Enumerable.Range(0, 4)
        .Select(
            row =>
                new
                {
                    index = row,
                    count = Enumerable.Range(0, 5).Select(col => arr[row, col]).Count(x => x == 1)
                })
        .OrderByDescending(x => x.count)
        .Select(x => x.index)
        .First();

这篇关于如何使用LINQ用2维阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆