Saturday 29 June 2013

LINQ Hour 5-3 Skip/SkipWhile and Take/TakeWhile in LINQ

Written By:-Isha Malhotra 
Our Website:-www.techaltum.com
Skip/SkipWhile and Take/TakeWhile
Skip

Skip in LINQ is used to skip values from the beginning. We pass a numeric value to skip which represent that how many values we need to skip.

For example

Skip with array

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

        var data_skip = (from res_skip in marks
                         orderby res_skip
                         select res_skip).Skip(3);
        Response.Write("Result using skip and skip count is 3 <br/>");
        foreach (int res_data_skip in data_skip)
        {
       
            Response.Write(res_data_skip+" ");
       
        }
Output



Figure 1

It simple arrange in ascending order and then skip 3 records as we gave 3 as input in the skip.

Take

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.

For Example:-

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

Skipwhile with array

We pass lambda expression to the skipwhile which is the condition. It will start skipping the value from the beginning till the condition will be satisfied. As it will find the record which do not match the condition it will stop skipping.

For Example:-

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

        var data_skip = (from res_skip in marks
                       
                         select res_skip).SkipWhile(x =>x%2!=0 );
        Response.Write("use of skip while <br/>");
        foreach (int res_data_skip in data_skip)
        {
       
            Response.Write(res_data_skip+" ");
       
        }
Output:-



Figure 2

As you can see that it skipped the value till it’s satisfying the condition as it encountered the value 78 which do not satisfy the condition so it stopped skipping the value and select the rest of the records.

TakeWhile

TakeWhile select the data till the condition will be true.

For Example:-

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 + " ");
       
        }
Output:-


Figure 3
           
Skip with Generics(List)

For Example:-

List<data> dt = new List<data>()
        {
            new data{roll_no=1, student="isha", per=100},
            new data{roll_no=2, student="sneha", per=89},
            new data{roll_no=3, student="rahul", per=34},
            new data{roll_no=4, student="renu", per=34},
            new data{roll_no=5, student="sapna", per=89}
        };

        //use of skip
        var res = (from record in dt

                   select record).Skip(1);
       
        Response.Write("Exampe of Skip <br/>");
       
        foreach (data skip_data in res)
        {

            Response.Write("Roll No:- " + skip_data.roll_no + " Student:- " + skip_data.student + "  per:- " + skip_data.per + "<br/>");
       
        }
Output:-



Figure 4

It simply skips one record from the beginning.

Similarly you can use take.

Use of SkipWhile with Genrics

For Example:-

List<data> dt = new List<data>()
        {
            new data{roll_no=1, student="isha", per=100},
            new data{roll_no=2, student="sneha", per=89},
            new data{roll_no=3, student="rahul", per=34},
            new data{roll_no=4, student="renu", per=34},
            new data{roll_no=5, student="sapna", per=89}
        };

        //use of skip while with two condition
        var res = (from record in dt

                   select record).SkipWhile(x => x.per > 50 && x.roll_no==1);
       
        Response.Write("Exampe of Skip while <br/>");
       
        foreach (data skip_data in res)
        {

            Response.Write("Roll No:- " + skip_data.roll_no + " Student:- " + skip_data.student + "  per:- " + skip_data.per + "<br/>");
       
        }
Output:-



Figure 5
As we have passed two conditions i.e roll number will be 1 and per will be greater than 50. So it will skipped the records till the condition is satisfying
Similarly we can use skip with database too.

For Example:-

var res = dc.GetTable<Class1>().Skip(3);
hope you enjoyed the article.



No comments:

Post a Comment