101 LINQ Samples: Partitioning Operators

Take - Simple

This sample uses Take to get only the first 3 elements of the array.

  1. public void Linq20()
  2.  
  3. {
  4.  
  5.     int[] numbers = { 5413986720 };
  6.  
  7.  
  8.  
  9.     var first3Numbers = numbers.Take(3);
  10.  
  11.  
  12.  
  13.     Console.WriteLine("First 3 numbers:");
  14.  
  15.     foreach (var n in first3Numbers)
  16.  
  17.     {
  18.  
  19.         Console.WriteLine(n);
  20.  
  21.     }
  22.  
  23. }

Result

First 3 numbers: 


1

Take - Nested

This sample uses Take to get the first 3 orders from customers in Washington.

  1. public void Linq21()
  2.  
  3. {
  4.  
  5.     List<Customer> customers = GetCustomerList();
  6.  
  7.  
  8.  
  9.     var first3WAOrders = (
  10.  
  11.         from c in customers
  12.  
  13.         from o in c.Orders
  14.  
  15.         where c.Region == "WA"
  16.  
  17.         select new { c.CustomerID, o.OrderID, o.OrderDate })
  18.  
  19.         .Take(3);
  20.  
  21.  
  22.  
  23.     Console.WriteLine("First 3 orders in WA:");
  24.  
  25.     foreach (var order in first3WAOrders)
  26.  
  27.     {
  28.  
  29.         ObjectDumper.Write(order);
  30.  
  31.     }
  32.  
  33. }

Result

First 3 orders in WA: 
CustomerID=LAZYK OrderID=10482 OrderDate=3/21/1997 
CustomerID=LAZYK OrderID=10545 OrderDate=5/22/1997 
CustomerID=TRAIH OrderID=10574 OrderDate=6/19/1997

Skip - Simple

This sample uses Skip to get all but the first 4 elements of the array.

  1. public void Linq22()
  2.  
  3. {
  4.  
  5.     int[] numbers = { 5413986720 };
  6.  
  7.  
  8.  
  9.     var allButFirst4Numbers = numbers.Skip(4);
  10.  
  11.  
  12.  
  13.     Console.WriteLine("All but first 4 numbers:");
  14.  
  15.     foreach (var n in allButFirst4Numbers)
  16.  
  17.     {
  18.  
  19.         Console.WriteLine(n);
  20.  
  21.     }
  22.  
  23. }

Result

All but first 4 numbers: 





0

Skip - Nested

This sample uses Take to get all but the first 2 orders from customers in Washington.

  1. public void Linq23()
  2.  
  3. {
  4.  
  5.     List<Customer> customers = GetCustomerList();
  6.  
  7.  
  8.  
  9.     var waOrders =
  10.  
  11.         from c in customers
  12.  
  13.         from o in c.Orders
  14.  
  15.         where c.Region == "WA"
  16.  
  17.         select new { c.CustomerID, o.OrderID, o.OrderDate };
  18.  
  19.  
  20.  
  21.     var allButFirst2Orders = waOrders.Skip(2);
  22.  
  23.  
  24.  
  25.     Console.WriteLine("All but first 2 orders in WA:");
  26.  
  27.     foreach (var order in allButFirst2Orders)
  28.  
  29.     {
  30.  
  31.         ObjectDumper.Write(order);
  32.  
  33.     }
  34.  
  35. }

Result

All but first 2 orders in WA:
CustomerID=TRAIH OrderID=10574 OrderDate=6/19/1997
CustomerID=TRAIH OrderID=10577 OrderDate=6/23/1997
CustomerID=TRAIH OrderID=10822 OrderDate=1/8/1998
CustomerID=WHITC OrderID=10269 OrderDate=7/31/1996
CustomerID=WHITC OrderID=10344 OrderDate=11/1/1996
CustomerID=WHITC OrderID=10469 OrderDate=3/10/1997
CustomerID=WHITC OrderID=10483 OrderDate=3/24/1997
CustomerID=WHITC OrderID=10504 OrderDate=4/11/1997
CustomerID=WHITC OrderID=10596 OrderDate=7/11/1997
CustomerID=WHITC OrderID=10693 OrderDate=10/6/1997
CustomerID=WHITC OrderID=10696 OrderDate=10/8/1997
CustomerID=WHITC OrderID=10723 OrderDate=10/30/1997
CustomerID=WHITC OrderID=10740 OrderDate=11/13/1997
CustomerID=WHITC OrderID=10861 OrderDate=1/30/1998
CustomerID=WHITC OrderID=10904 OrderDate=2/24/1998
CustomerID=WHITC OrderID=11032 OrderDate=4/17/1998
CustomerID=WHITC OrderID=11066 OrderDate=5/1/1998

TakeWhile - Simple

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

  1. public void Linq24()
  2.  
  3. {
  4.  
  5.     int[] numbers = { 5413986720 };
  6.  
  7.  
  8.  
  9.     var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);
  10.  
  11.  
  12.  
  13.     Console.WriteLine("First numbers less than 6:");
  14.  
  15.     foreach (var n in firstNumbersLessThan6)
  16.  
  17.     {
  18.  
  19.         Console.WriteLine(n);
  20.  
  21.     }
  22.  
  23. }

Result

First numbers less than 6:
5
4
1
3

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.

  1. public void Linq25()
  2.  
  3. {
  4.  
  5.     int[] numbers = { 5413986720 };
  6.  
  7.  
  8.  
  9.     var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index);
  10.  
  11.  
  12.  
  13.     Console.WriteLine("First numbers not less than their position:");
  14.  
  15.     foreach (var n in firstSmallNumbers)
  16.  
  17.     {
  18.  
  19.         Console.WriteLine(n);
  20.  
  21.     }
  22.  
  23. }

Result

First numbers not less than their position: 

4

SkipWhile - Simple

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

  1. public void Linq26()
  2.  
  3. {
  4.  
  5.     int[] numbers = { 5413986720 };
  6.  
  7.  
  8.  
  9.     var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0);
  10.  
  11.  
  12.  
  13.     Console.WriteLine("All elements starting from first element divisible by 3:");
  14.  
  15.     foreach (var n in allButFirst3Numbers)
  16.  
  17.     {
  18.  
  19.         Console.WriteLine(n);
  20.  
  21.     }
  22.  
  23. }

Result

All elements starting from first element divisible by 3:
3
9
8
6
7
2
0

SkipWhile - Indexed

This sample uses SkipWhile to get the elements of the array starting from the first element less than its position.

  1. public void Linq27()
  2.  
  3. {
  4.  
  5.     int[] numbers = { 5413986720 };
  6.  
  7.  
  8.  
  9.     var laterNumbers = numbers.SkipWhile((n, index) => n >= index);
  10.  
  11.  
  12.  
  13.     Console.WriteLine("All elements starting from first element less than its position:");
  14.  
  15.     foreach (var n in laterNumbers)
  16.  
  17.     {
  18.  
  19.         Console.WriteLine(n);
  20.  
  21.     }
  22.  
  23. }

Result

All elements starting from first element less than its position:
1
3
9
8
6
7
2
0

posted @ 2011-03-13 19:44  i'm zjz  阅读(318)  评论(0编辑  收藏  举报