int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var lowNums = from n in numbers where n < 5 select n;
List<Product> products = GetProductList(); var soldOutProducts = from p in products where p.UnitsInStock == 0 select p;
List<Product> products = GetProductList(); var expensiveInStockProducts = from p in products where p.UnitsInStock > 0 && p.UnitPrice > 3.00M select p;
List<Customer> customers = GetCustomerList(); var waCustomers = from c in customers where c.Region == "WA" select c;
string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var shortDigits = digits.Where((digit, index) => digit.Length < index);
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numsPlusOne = from n in numbers select n + 1;
List<Product> products = GetProductList(); var productNames = from p in products select p.ProductName;
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var textNums = from n in numbers select strings[n];
string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" }; var upperLowerWords = from w in words select new { Upper = w.ToUpper(), Lower = w.ToLower() };
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var digitOddEvens = from n in numbers select new { Digit = strings[n], Even = (n % 2 == 0) };
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) });
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; var lowNums = from n in numbers where n < 5 select digits[n];
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; var pairs = from a in numbersA from b in numbersB where a < b select new { a, b };
List<Customer> customers = GetCustomerList(); var orders = from c in customers from o in c.Orders where o.Total < 500.00M select new { c.CustomerID, o.OrderID, o.Total };
List<Customer> customers = GetCustomerList(); var orders = from c in customers from o in c.Orders where o.OrderDate >= new DateTime(1998, 1, 1) select new { c.CustomerID, o.OrderID, o.OrderDate };
List<Customer> customers = GetCustomerList(); var orders = from c in customers from o in c.Orders where o.Total >= 2000.0M select new { c.CustomerID, o.OrderID, o.Total };
List<Customer> customers = GetCustomerList(); DateTime cutoffDate = new DateTime(1997, 1, 1); var orders = from c in customers where c.Region == "WA" from o in c.Orders where o.OrderDate >= cutoffDate select new { c.CustomerID, o.OrderID };
List<Customer> customers = GetCustomerList(); var customerOrders = customers.SelectMany( (cust, custIndex) => cust.Orders.Select(o => "Customer #" + (custIndex + 1) + " has an order with OrderID " + o.OrderID));
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var first3Numbers = numbers.Take(3);
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var allButFirst4Numbers = numbers.Skip(4);
All but first 4 numbers:
9
8
6
7
2
0
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);
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var firstNumbersLessThan6 = numbers.TakeWhile(n => n < 6);
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var firstSmallNumbers = numbers.TakeWhile((n, index) => n >= index);
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var allButFirst3Numbers = numbers.SkipWhile(n => n % 3 != 0);
3
9
8
6
7
2
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var laterNumbers = numbers.SkipWhile((n, index) => n >= index);
string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" }; var wordGroups = from w in words group w by w[0] into g select new { FirstLetter = g.Key, Words = g }; foreach (var g in wordGroups) { Console.WriteLine("Words that start with the letter '{0}':", g.FirstLetter); foreach (var w in g.Words) { Console.WriteLine(w); } }
List<Product> products = GetProductList(); var orderGroups = from p in products group p by p.Category into g select new { Category = g.Key, Products = g }; ObjectDumper.Write(orderGroups, 1);
List<Customer> customers = GetCustomerList(); var customerOrderGroups = from c in customers select new { c.CompanyName, YearGroups = from o in c.Orders group o by o.OrderDate.Year into yg select new { Year = yg.Key, MonthGroups = from o in yg group o by o.OrderDate.Month into mg select new { Month = mg.Key, Orders = mg } } };
GroupBy - Comparer
This sample uses GroupBy to partition trimmed elements of an array using a custom comparer that matches words that are anagrams of each other.
1. public void Linq44()
2. {
3. string[] anagrams = { "from ", " salt", " earn ", " last ", " near ", " form " };
4.
5. var orderGroups = anagrams.GroupBy(w => w.Trim(), new AnagramEqualityComparer());
6.
7. ObjectDumper.Write(orderGroups, 1);
8. }
9.
10. public class AnagramEqualityComparer : IEqualityComparer<string>
11. {
12. public bool Equals(string x, string y)
13. {
14. return getCanonicalString(x) == getCanonicalString(y);
15. }
16.
17. public int GetHashCode(string obj)
18. {
19. return getCanonicalString(obj).GetHashCode();
20. }
21.
22. private string getCanonicalString(string word)
23. {
24. char[] wordChars = word.ToCharArray();
25. Array.Sort<char>(wordChars);
26. return new string(wordChars);
27. }
28. }
Result
...
from
form
...
salt
last
...
earn
near
GroupBy - Comparer, Mapped
This sample uses GroupBy to partition trimmed elements of an array using a custom comparer that matches words that are anagrams of each other, and then converts the results to uppercase.
1. public void Linq45()
2. {
3. string[] anagrams = { "from ", " salt", " earn ", " last ", " near ", " form " };
4.
5. var orderGroups = anagrams.GroupBy(
6. w => w.Trim(),
7. a => a.ToUpper(),
8. new AnagramEqualityComparer()
9. );
10.
11. ObjectDumper.Write(orderGroups, 1);
12. }
13.
14. public class AnagramEqualityComparer : IEqualityComparer<string>
15. {
16. public bool Equals(string x, string y)
17. {
18. return getCanonicalString(x) == getCanonicalString(y);
19. }
20.
21. public int GetHashCode(string obj)
22. {
23. return getCanonicalString(obj).GetHashCode();
24. }
25.
26. private string getCanonicalString(string word)
27. {
28. char[] wordChars = word.ToCharArray();
29. Array.Sort<char>(wordChars);
30. return new string(wordChars);
31. }
32. }
Result
...
FROM
FORM
...
SALT
LAST
...
EARN
NEAR