Partitioning Operators
Take - Simple
This sample uses Take to get only the first 3 elements of the array.
Code:
public void Linq20()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var first3Numbers = numbers.Take(3);
Log.WriteLine("First 3 numbers:");
foreach (var n in first3Numbers)
{
Log.WriteLine(n);
}
}
Result:
Take - Nested
This sample uses Take to get the first 3 orders from customers in Washington.
Code:
public void Linq21()
{
List customers = GetCustomerList();
var first3WAOrders =
(customers.SelectMany(cust => cust.Orders, (cust, order) => new {cust, order}).
Where(@t => @t.cust.Region == "WA").Select(
@t => new {@t.cust.CustomerID, @t.order.OrderID, @t.order.OrderDate}))
.Take(3);
Log.WriteLine("First 3 orders in WA:");
foreach (var order in first3WAOrders)
{
ObjectDumper.Write(order);
}
}
Result:
Skip - Simple
This sample uses Skip to get all but the first four elements of the array.
Code:
public void Linq22()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var allButFirst4Numbers = numbers.Skip(4);
Log.WriteLine("All but first 4 numbers:");
foreach (var n in allButFirst4Numbers)
{
Log.WriteLine(n);
}
}
Result:
Skip - Nested
This sample uses Take to get all but the first 2 orders from customers in Washington.
Code:
public void Linq23()
{
List customers = GetCustomerList();
var waOrders =
customers.SelectMany(cust => cust.Orders, (cust, order) => new {cust, order}).Where(
@t => @t.cust.Region == "WA").Select(
@t => new {@t.cust.CustomerID, @t.order.OrderID, @t.order.OrderDate});
var allButFirst2Orders = waOrders.Skip(2);
Log.WriteLine("All but first 2 orders in WA:");
foreach (var order in allButFirst2Orders)
{
ObjectDumper.Write(order);
}
}
Result:
TakeWhile - Simple
This sample uses TakeWhile to return elements starting from the beginning of the array until a number is read whose value is not less than 6.
Code:
public void Linq24()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);
Log.WriteLine("First numbers less than 6:");
foreach (var num in firstNumbersLessThan6)
{
Log.WriteLine(num);
}
}
Result:
TakeWhile - Indexed
This sample uses TakeWhile to return elements starting from the beginning of the array until a number is hit that is less than its position in the array.
Code:
public void Linq25()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index);
Log.WriteLine("First numbers not less than their position:");
foreach (var n in firstSmallNumbers)
{
Log.WriteLine(n);
}
}
Result:
SkipWhile - Simple
This sample uses SkipWhile to get the elements of the array starting from the first element divisible by 3.
Code:
public void Linq26()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
// In the lambda expression, 'n' is the input parameter that identifies each
// element in the collection in succession. It is is inferred to be
// of type int because numbers is an int array.
var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0);
Log.WriteLine("All elements starting from first element divisible by 3:");
foreach (var n in allButFirst3Numbers)
{
Log.WriteLine(n);
}
}
Result:
SkipWhile - Indexed
This sample uses SkipWhile to get the elements of the array starting from the first element less than its position.
Code:
public void Linq27()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var laterNumbers = numbers.SkipWhile((n, index) => n >= index);
Log.WriteLine("All elements starting from first element less than its position:");
foreach (var n in laterNumbers)
{
Log.WriteLine(n);
}
}
Result: