SelectMany - Multiple from
public void Linq18() {
List customers = GetCustomerList();
DateTime cutoffDate = new DateTime(1997, 1, 1); var orders =
from c in customers where c.Region == \ from o in c.Orders
where o.OrderDate >= cutoffDate select new {c.CustomerID, o.OrderID}; ObjectDumper.Write(orders); }
SelectMany - Indexed
public void Linq19() {
List customers = GetCustomerList(); var customerOrders =
customers.SelectMany( (cust, custIndex) =>
cust.Orders.Select(o => \
\ ObjectDumper.Write(customerOrders); }
Partitioning Operators Take - Simple
public void Linq20() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var first3Numbers = numbers.Take(3); Console.WriteLine(\ foreach (var n in first3Numbers) { Console.WriteLine(n); } }
Take - Nested public void Linq21() {
List
where c.Region == \
select new {c.CustomerID, o.OrderID, o.OrderDate} ) .Take(3);
Console.WriteLine(\ foreach (var order in first3WAOrders) { ObjectDumper.Write(order); } }
Skip - Simple
public void Linq22() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var allButFirst4Numbers = numbers.Skip(4); Console.WriteLine(\ foreach (var n in allButFirst4Numbers) { Console.WriteLine(n); } }
Skip - Nested
public void Linq23() {
List
from c in customers
from o in c.Orders
where c.Region == \
select new {c.CustomerID, o.OrderID, o.OrderDate}; var allButFirst2Orders = waOrders.Skip(2); Console.WriteLine(\ foreach (var order in allButFirst2Orders) { ObjectDumper.Write(order); } }
TakeWhile - Simple
public void Linq24() {
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6); Console.WriteLine(\ foreach (var n in firstNumbersLessThan6) { Console.WriteLine(n); } }
SkipWhile - Simple 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(\ foreach (var n in allButFirst3Numbers) { Console.WriteLine(n); } }
SkipWhile - Indexed 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(\elements starting from first element less than position:\
foreach (var n in laterNumbers) { Console.WriteLine(n); } }
Ordering Operators OrderBy - Simple 1 publicvoid Linq28() {
string[] words = { \
var sortedWords = from w in words orderby w select w;
Console.WriteLine(\ foreach (var w in sortedWords) { Console.WriteLine(w); } }
OrderBy - Simple 2 public void Linq29() {
string[] words = { \ var sortedWords = from w in words orderby w.Length select w;
Console.WriteLine(\ foreach (var w in sortedWords) { Console.WriteLine(w);
its }
}
OrderBy - Simple 3 public void Linq30() {
List products = GetProductList(); var sortedProducts = from p in products
orderby p.ProductName select p;
ObjectDumper.Write(sortedProducts); }
OrderBy - Comparer
public class CaseInsensitiveComparer : IComparer
public int Compare(string x, string y) {
return string.Compare(x, y, true); } }
public void Linq31() {
string[] words = { \ var sortedWords = words.OrderBy(a => a, new CaseInsensitiveComparer()); ObjectDumper.Write(sortedWords); }
OrderByDescending - Simple 1 public void Linq32() {
double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; var sortedDoubles = from d in doubles orderby d descending select d;
Console.WriteLine(\ foreach (var d in sortedDoubles) { Console.WriteLine(d); } }
OrderByDescending - Simple 2 public void Linq33() {
List products = GetProductList(); var sortedProducts =
from p in products
orderby p.UnitsInStock descending select p;
ObjectDumper.Write(sortedProducts); }
OrderByDescending - Comparer
public class CaseInsensitiveComparer : IComparerspan class=\{
publicint Compare(string x, string y) {
returnstring.Compare(x, y, true); } }
publicvoid Linq34() {
string[] words = { \
var sortedWords = words.OrderByDescending(a => a, new CaseInsensitiveComparer());
ObjectDumper.Write(sortedWords); }
ThenBy - Simple publicvoid Linq35() {
string[] digits = { \
var sortedDigits = from d in digits orderby d.Length, d select d;
Console.WriteLine(\ foreach (var d in sortedDigits) { Console.WriteLine(d); } }
ThenBy - Comparer
public class CaseInsensitiveComparer : IComparerspan class=\{
publicint Compare(string x, string y) {
returnstring.Compare(x, y, true);