Written By:- Isha Malhotra
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.
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
");
foreach (int res_data_skip in data_skip)
{
Response.Write(res_data_skip+" ");
}
It will first arrange in ascending order and then skip 3 records as we gave 3 as input in the skip.
Output:-
Figure 1
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.
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+" ");
}
The output of this code is as follows:-
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.
For any query you can send mail at info@techaltum.com
Thanks