Linq Hour 5 3 Skipskipwhile And

Embed Size (px)

Citation preview

  • 7/27/2019 Linq Hour 5 3 Skipskipwhile And

    1/6

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

    Skip/SkipWhile and Take/TakeWhile

    SkipSkip in LINQ is used to skip values from the beginning. We pass anumeric value to skip which represent that how many values we need toskip.

    For example

    Skip with array

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

    var data_skip = (from res_skip in marksorderby res_skip

    select res_skip).Skip(3);Response.Write("Result using skip and skip count

    is 3
    ");

    foreach (int res_data_skip in data_skip)

    {

    Response.Write(res_data_skip+" ");

    }Output

    Figure 1

    http://1.bp.blogspot.com/-HMkEnPu2n0w/Uc89-v0ej7I/AAAAAAAAAJA/XDx9fm8DSDw/s340/skip-1.png
  • 7/27/2019 Linq Hour 5 3 Skipskipwhile And

    2/6

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

    Take

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

    For Example:-

    int[] marks = newint[] { 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. Itwill start skipping the value from the beginning till the condition will besatisfied. As it will find the record which do not match the condition it willstop skipping.

    For Example:-

    int[] marks = newint[] { 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
    ");

    foreach (int res_data_skip in data_skip)

  • 7/27/2019 Linq Hour 5 3 Skipskipwhile And

    3/6

    {

    Response.Write(res_data_skip+" ");

    }Output:-

    Figure 2

    As you can see that it skipped the value till its satisfying the condition asit encountered the value 78 which do not satisfy the condition so itstopped 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 = newint[] { 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
    ");foreach (int res_data_take in data_take)

    {

    Response.Write(res_data_take + " ");

    }

    http://1.bp.blogspot.com/-AHq8yz0VHak/Uc8-LpigO1I/AAAAAAAAAJI/HLAk2gTsDOc/s322/skip-2.png
  • 7/27/2019 Linq Hour 5 3 Skipskipwhile And

    4/6

    Output:-

    Figure 3

    Skip with Generics(List)

    For Example:-

    List dt = newList()

    {newdata{roll_no=1, student="isha", per=100},

    newdata{roll_no=2, student="sneha", per=89},

    newdata{roll_no=3, student="rahul", per=34},newdata{roll_no=4, student="renu", per=34},

    newdata{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
    ");

    foreach (data skip_data in res)

    {

    Response.Write("Roll No:- " +

    skip_data.roll_no + " Student:- " + skip_data.student +" per:- " + skip_data.per + "
    ");

    }Output:-

    http://1.bp.blogspot.com/-YCsBO3olBtA/Uc8-WM9Rv6I/AAAAAAAAAJQ/_7tW9s23e7Y/s353/skip-3.png
  • 7/27/2019 Linq Hour 5 3 Skipskipwhile And

    5/6

    Figure 4

    It simply skips one record from the beginning.

    Similarly you can use take.

    Use of SkipWhile with Genrics

    For Example:-

    List dt = newList(){

    newdata{roll_no=1, student="isha", per=100},newdata{roll_no=2, student="sneha", per=89},

    newdata{roll_no=3, student="rahul", per=34},newdata{roll_no=4, student="renu", per=34},

    newdata{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
    ");

    http://4.bp.blogspot.com/-8wQV-S7a2D8/Uc8-fkV7_qI/AAAAAAAAAJY/m02zAstBHXs/s336/skip-4.png
  • 7/27/2019 Linq Hour 5 3 Skipskipwhile And

    6/6

    foreach (data skip_data in res)

    {

    Response.Write("Roll No:- " +

    skip_data.roll_no + " Student:- " + skip_data.student +" per:- " + skip_data.per + "
    ");

    }Output:-

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

    For Example:-

    var res = dc.GetTable().Skip(3);

    hope you enjoyed the article.

    TECH ALTUM. Picture Window template. Powered byBlogger.

    http://www.blogger.com/http://www.blogger.com/http://www.blogger.com/http://3.bp.blogspot.com/-igJ4uIhCPvM/Uc8-sHbWqYI/AAAAAAAAAJg/FGSDs8eN4h4/s324/skip-5.pnghttp://www.blogger.com/