Jagged Array in LINQ


What is Jagged Array

Jagged array is array of array. We can also traverse jagged array using LINQ.

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

Traversal of Jagged Array

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

No comments:

Post a Comment