Thursday, 25 July 2013

Jagged array using LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com                                         
Jagged array using LINQ


We can also traverse jagged array using LINQ.

Following is the jagged array:-

int[][] jarray =new int[3][];
        jarray[0] = new int[3] { 4, 5, 6 };
        jarray[1] = new int[2] { 7, 8 };
        jarray[2] = new int[4] { 9, 10, 34, 12 };

Suppose I have to show all even elements then we will
Traverse the jagged array using LINQ in following manner:-

int[][] jarray =new int[3][];
        jarray[0] = new int[3] { 4, 5, 6 };
        jarray[1] = new int[2] { 7, 8 };
        jarray[2] = new int[4] { 9, 10, 34, 12 };

        IEnumerable<int> res = from jar in jarray
                               from res_jar in jar
                               where res_jar % 2 == 0
                               select res_jar;

        foreach (int res_data in res)
        {

            Response.Write(res_data);
       
        }


Another way to traverse the jagged array using LINQ

for (int i = 0; i < jarray.Length; i++)
        {

            IEnumerable<int> res_for = from jar in jarray[i]
                                       where jar % 2 == 0
                                       select jar;

            foreach (int res_data in res_for)
            {

                Response.Write(res_data);

            }
       
        }

Similarly you can traverse the 2-d and 3-d array using LINQ.

1 comment:

  1. Really something Grate in this article Thanks for sharing this. We are providing Online Training Classes. After reading this slightly I am changed my way of introduction about my training to people.

    Best Linux training in Noida
    Linux Training Institute in Noida

    ReplyDelete