Showing posts with label jagged array in linq. Show all posts
Showing posts with label jagged array in linq. Show all posts

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);
       
        }