Take in LINQ

Take is used to take the value from the beginning according to your count. It will work like top command which we use in SQL.

Example of Take in LINQ

		
	  int[] marks = new int[] { 23, 45, 78, 90, 56, 89, 10, 32 };

        //it will take first five value from the array
        var data_take = (from res_take in marks

                         select res_take).Take(5);
      
        foreach (int res_data_take in data_take)
        {

            Response.Write(res_data_take + " ");
       
        }
		
		

TakeWhile in LINQ

TakeWhile select the data till the condition will be true.

		
		int[] marks = new int[] { 23, 45, 78, 90, 56, 89, 10, 32 };

        //it will take first five value from the array
        var data_take = (from res_take in marks

                         select res_take).TakeWhile(x => x % 2 != 0);
        Response.Write("use of take while <br/>");
        foreach (int res_data_take in data_take)
        {

            Response.Write(res_data_take + " ");
       
        }


	
		

The output of this code is as follows:-

		
		takewhile example

		Figure 1

		
		

Email Address

For any query you can send mail at info@techaltum.com
Thanks