locked
How should I put where clause for two dimensional array using linq?

    Question

  •    How should I put where clause for two dimensional array using linq?

     

    For one dimensional array we can write :

    cmbData.DataSource = arr

                cmbData.DataSource = From n In arr _

                Where (n > 5) _

                      Select

     

    I want to query two dimensional array using where clause

    What should I write in where clause ?

     

    Friday, September 26, 2008 7:34 AM

Answers

  • Hi Prajakta,

    Based on my investigation, the rectangular array is non-enumerable and non-queryable collection, you can try to test the following code snippet to verify it, though it is C# code, but VB.Net also need to implement “IEnumerable” or “IQueryable” so that LINQ can work, so LINQ cannot query rectangular.


      //create rectangular array

                int[,] arrays = { {1,2},{3,4},{5,6}};

     

                if (arrays is IEnumerable<int[]>)

                    Console.WriteLine("Enumerable");

     

                if (arrays is IEnumerable<int>)

                    Console.WriteLine("Enumerable");

     

                if (arrays is IQueryable<int[]>)

                    Console.WriteLine("IQueryable");

     

                if (arrays is IQueryable<int>)

                    Console.WriteLine("IQueryable");


    Therefore, in order to query a rectangular array, one suggestion will pass the values to a enumerable collection, such as a regular one-dimensional array.


    As the jagged array, it is an Enumerable for “integer []” element, to query a “integer” element, one suggestion will be to query the element of the “int[]” from the jagged array, in addition to this, please refer to the following code snippet.


      // create some jagged data

                int[][] data = new int[ 3 ][];

                for (int i = 0; i < 3; i++)

                {

                    int width;

                    switch (i)

                    { // true jagged

                        case 0: width = 5; break;

                        case 1: width = 3; break;

                        case 2: width = 7; break;

                        default: width = 1; break;

                    }

                    data[ i ] = new int[width];

                    for (int j = 0; j < width; j++)

                    {

                        data[ i ][ j ] = 20 * i + j;

                    }

                }

     

                if (data is IEnumerable<int[]>)

                    Console.WriteLine("Enumerable");

     

     

                var qs1 = from row in data

                          select row;

                foreach (var q1 in qs1)

                {

                    var q11 = from n in q1

                              where n == 20

                              select n;

                    foreach (int qr in q11)

                        Console.WriteLine(qr);

                }

            }

     

    Regards,

    Xun

    Tuesday, September 30, 2008 3:55 AM

All replies

  • Is the array rectangular or jagged?

    If it's rectangular, you can query as for a one-dimensional array.

    If it's jagged, then you've got an array of arrays, so you'll need to use either SelectMany or multiple from clauses to do the query.

    Joe
    Friday, September 26, 2008 1:44 PM
  • Thanks for your reply

    can you please exaplain with example

    Monday, September 29, 2008 5:13 AM
  • Hi Prajakta,

    Based on my investigation, the rectangular array is non-enumerable and non-queryable collection, you can try to test the following code snippet to verify it, though it is C# code, but VB.Net also need to implement “IEnumerable” or “IQueryable” so that LINQ can work, so LINQ cannot query rectangular.


      //create rectangular array

                int[,] arrays = { {1,2},{3,4},{5,6}};

     

                if (arrays is IEnumerable<int[]>)

                    Console.WriteLine("Enumerable");

     

                if (arrays is IEnumerable<int>)

                    Console.WriteLine("Enumerable");

     

                if (arrays is IQueryable<int[]>)

                    Console.WriteLine("IQueryable");

     

                if (arrays is IQueryable<int>)

                    Console.WriteLine("IQueryable");


    Therefore, in order to query a rectangular array, one suggestion will pass the values to a enumerable collection, such as a regular one-dimensional array.


    As the jagged array, it is an Enumerable for “integer []” element, to query a “integer” element, one suggestion will be to query the element of the “int[]” from the jagged array, in addition to this, please refer to the following code snippet.


      // create some jagged data

                int[][] data = new int[ 3 ][];

                for (int i = 0; i < 3; i++)

                {

                    int width;

                    switch (i)

                    { // true jagged

                        case 0: width = 5; break;

                        case 1: width = 3; break;

                        case 2: width = 7; break;

                        default: width = 1; break;

                    }

                    data[ i ] = new int[width];

                    for (int j = 0; j < width; j++)

                    {

                        data[ i ][ j ] = 20 * i + j;

                    }

                }

     

                if (data is IEnumerable<int[]>)

                    Console.WriteLine("Enumerable");

     

     

                var qs1 = from row in data

                          select row;

                foreach (var q1 in qs1)

                {

                    var q11 = from n in q1

                              where n == 20

                              select n;

                    foreach (int qr in q11)

                        Console.WriteLine(qr);

                }

            }

     

    Regards,

    Xun

    Tuesday, September 30, 2008 3:55 AM
  • Two forms for selectmany

     

    from n in arr

    from i in n

    where i > 5

    select i

     

     

    arr.SelectMany(n => n).Where(i => i > 5)

     

    Wednesday, November 05, 2008 1:01 PM