Linqpartition

[Back to LINQ examples] View Original LINQ Code

Take - Simple

This sample prints the first 3 elements of the array.

This is one of the LINQ examples designed to show LINQ operating over standard Java collections; it uses the Take operator. In this case the batch version is just ordinary Java iteration.

public void Batch20() {
  int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  for (int i=0; i<3; i++)
    print("{0}", numbers[i]);
}

Original LINQ code:

public void Linq20()
{
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

    var first3Numbers = numbers.Take(3);

    Console.WriteLine("First 3 numbers:");

    foreach (var n in first3Numbers)
    {
        Console.WriteLine(n);
    }
}

Take - Nested

This sample prints the first 3 orders from customers in Washington.

The LINQ version uses Take. The batch version uses first.

public void Batch21() {
  print("First 3 orders in WA:");
  for (Northwind db : connection)
    for (Order o : db.Orders.first(3))
      if (o.Customer.Region == "WA")
        print("CustomerID={0} OrderID={1} OrderDate={2}",
               o.Customer.CustomerID,
               o.OrderID,
               o.OrderDate);
}

Generated SQL code:

SELECT 
  T2.CustomerID AS g0,
  T1.OrderID AS g1,
  T1.OrderDate AS g2
FROM Orders T1
INNER JOIN Customers T2 ON (T2.CustomerID=T1.CustomerID)
WHERE (T2.Region="WA")
LIMIT 3

Original LINQ code:

private void Linq21()
{
    List<Customer> customers = GetCustomerList();
    var first3WAOrders = (
        from c in customers
        from o in c.Orders
        where c.Region == "WA"
        select new { c.CustomerID, o.OrderID, o.OrderDate })
        .Take(3);
    Console.WriteLine("First 3 orders in WA:");
    foreach (var order in first3WAOrders)
    {
        ObjectDumper.Write(order);
    }
}

Skip - Simple

This sample prints all but the first 4 elements of the array.

This is one of the LINQ examples designed to show LINQ operating over standard Java collections; it uses the Skip operator. In this case the batch version is just ordinary Java iteration.

public void Batch22() {
  int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  print("All but first 4 numbers:");
  for (int i=4; i<numbers.length; i++)
    print("{0}", numbers[i]);
}

Original LINQ code:

public void Linq22()
{
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
    var allButFirst4Numbers = numbers.Skip(4);
    Console.WriteLine("All but first 4 numbers:");

    foreach (var n in allButFirst4Numbers)
    {
        Console.WriteLine(n);
    }
}

Skip - Nested

This sample gets all but the first 2 orders from customers in Washington

public void Batch23() {
   int index = 0;
   for (Northwind db : connection) {
     print("All but first 2 orders in WA:");
     for (Customer c : db.Customers) 
       if (c.Region == "WA") 
         for (Order o : c.Orders)
           if (index > 1)
             print("CustomerID={0} OrderID={1} OrderDate={2}",
                 o.Customer.CustomerID,
                 o.OrderID,
                 o.OrderDate);
           else 
             index++;            
   }
}

Original LINQ code:

void Linq23()
{
    List<Customer> customers = GetCustomerList();
    var waOrders =
        from c in customers
        from o in c.Orders
        where c.Region == "WA"
        select new { c.CustomerID, o.OrderID, o.OrderDate };
    var allButFirst2Orders = waOrders.Skip(2);
    Console.WriteLine("All but first 2 orders in WA:");
    foreach (var order in allButFirst2Orders)
    {
        ObjectDumper.Write(order);
    }
}

TakeWhile - Simple

This sample return elements starting from the beginning of the array until a number is hit that is not less than 6.

public void Batch24() {
  int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  print("First numbers less than 6:");
  for (int i=0; i<numbers.length; i++) {
    if (numbers[i] > 6)
      break;
    print("{0}", numbers[i]);
  }
}

Original LINQ code:

public void Linq24()
{
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
    var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);
    Console.WriteLine("First numbers less than 6:");
    foreach (var n in firstNumbersLessThan6)
        Console.WriteLine(n);
}

TakeWhile - Indexed

This sample returns elements starting from the beginning of the array until a number is hit that is less than its position in the array.

public void Batch25() {
  int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  print("First numbers not less than their position:");
  for (int i=0; i<numbers.length; i++) {
    if (numbers[i] < i)
      break;
    print("{0}", numbers[i]);
  }
}

Original LINQ code:

public void Linq25()
{
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
    var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index);
    Console.WriteLine("First numbers not less than their position:");
    foreach (var n in firstSmallNumbers)
        Console.WriteLine(n);
}

SkipWhile - Simple

This sample uses SkipWhile to get the elements of the array starting from the first element divisible by 3.

public void Batch26() {
  int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  print("All elements starting from first element divisible by 3:");
  int i = 0;
  while (numbers[i] % 3 != 0)
    i++;
  for (; i<numbers.length; i++) {
    print("{0}", numbers[i]);
  }
}

Original LINQ code:

public void Linq26()
{
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
    var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0);
    Console.WriteLine("All elements starting from first element divisible by 3:");
    foreach (var n in allButFirst3Numbers)
        Console.WriteLine(n);
}

SkipWhile - Indexed

This sample gets the elements of the array starting from the first element less than its position.

public void Batch27() {
  int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
  print("All elements starting from first element less than its position:");
  int i=0;
  while (numbers[i] >= i)
    i++;
  for (; i<numbers.length; i++) {
    print("{0}", numbers[i]);
  }
}

Original LINQ code:

public void Linq27()
{
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
    var laterNumbers = numbers.SkipWhile((n, index) => n >= index);
    Console.WriteLine("All elements starting from first element less than its position:");
    foreach (var n in laterNumbers)
        Console.WriteLine(n);
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License