1. What is the Take, Skip operator?
2. What is the signature of the Take, Skip operator?
3. What is the TakeWhile, SkipWhile operator?
4. What is the signature of the TakeWhile, SkipWhile operator?
We can use the Partioning operators when we need to take only subset of the source sequence.
The Take (Skip) operator takes(skips) from the source sequence the count number of elements given by the count parameter.
public static IEnumerable<TSource> Skip<TSource>( this IEnumerable<TSource> source, int count);
public static bool Any<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate);
Example 1:
var top3Customers = (from gr in
(from amount in
(from product in Data.Products
join order in
(from c in Data.Customers
from o in c.Orders
select new { c.Name, Order = o }) on product.IdProduct equals order.Order.IdProduct
select new { order.Name, OrderAmount = order.Order.Quantity * product.Price })
group amount by amount.Name)
let TotalAmount = gr.Sum(o => o.OrderAmount)
orderby TotalAmount descending
select gr).Take(3);
foreach (var item in top3Customers)
{
Trace.WriteLine(item.Key);
foreach (var data in item)
{
Trace.WriteLine(data);
}
}
Output:
James
{ Name = James, OrderAmount = 600 }
{ Name = James, OrderAmount = 600 }
{ Name = James, OrderAmount = 600 }
Frank
{ Name = Frank, OrderAmount = 1000 }
Marco
{ Name = Marco, OrderAmount = 100 }
{ Name = Marco, OrderAmount = 600 }
Example 2:
var skipped3 = Data.Customers.Skip(3);
Output:
Frank – Seattle – USA
The TakesWhile operator enumerates elements in the source sequence and put them into new Enumerable object till the predicate function satisfy conditionals.
Example 3:
decimal globalAmount = (from p in Data.Products
join data in
(from c in Data.Customers
from o in c.Orders
select new { o.IdProduct, o.Quantity })
on p.IdProduct equals data.IdProduct into porder
select new { Total = porder.Sum(o => o.Quantity * p.Price) }.Total).Sum();
var limitAmount = (double)Decimal.Multiply(globalAmount, (decimal)0.5);
var Total = 0.0;
var halfProfitCustomers = ((from c in Data.Customers
join order in
(from p in Data.Products
join order in
(from c in Data.Customers
from o in c.Orders
select new { c.Name, o.IdProduct, o.Quantity })
on p.IdProduct equals order.IdProduct
select new { order.Name, OrderAmount = order.Quantity * p.Price })
on c.Name equals order.Name into corder
select new { c.Name, TotalAmount = corder.Sum(o => o.OrderAmount) }).OrderByDescending(o => o.TotalAmount))
.TakeWhile(x => { bool result = Total < limitAmount; Total += (double)x.TotalAmount; return result; });
foreach (var item in halfProfitCustomers)
{
Trace.WriteLine(item);
}
Output:
{ Name = James, TotalAmount = 1800 }
{ Name = Frank, TotalAmount = 1000 }
The SkipWhile operator enumerates elements in the source sequence and skip them till the predicate function satisfy conditionals, leftover elements will be put into a new enumerable object.
Example 4:
var getBadCustomers = ((from c in Data.Customers
join order in
(from p in Data.Products
join order in
(from c in Data.Customers
from o in c.Orders
select new { c.Name, o.IdProduct, o.Quantity })
on p.IdProduct equals order.IdProduct
select new { order.Name, OrderAmount = order.Quantity * p.Price })
on c.Name equals order.Name into corder
select new { c.Name, TotalAmount = corder.Sum(o => o.OrderAmount) }).OrderByDescending(o => o.TotalAmount))
.SkipWhile(x => { bool result = Total < limitAmount; Total += (double)x.TotalAmount; return result; });
Output:
{ Name = Marco, TotalAmount = 700 }
{ Name = Paolo, TotalAmount = 130 }