Quantifiers
Any - Simple
This sample uses Any to determine if any of the words in the array contain the substring 'ei'.Code:
public void Linq67() { string[] words = { "believe", "relief", "receipt", "field" }; bool iAfterE = words.Any(w => w.Contains("ei")); //DONE fixed typo in writeline Log.WriteLine("There is a word in the list that contains 'ei': {0}", iAfterE); }
Result:
Any - Grouped
This sample uses Any to return a grouped a list of products only for categories that have at least one product that is out of stock.Code:
public void Linq69() { Listproducts = GetProductList(); var productGroups = products.GroupBy(prod => prod.Category) .Where(prodGroup => prodGroup.Any(p => p.UnitsInStock == 0)) .Select(prodGroup => new { Category = prodGroup.Key, Products = prodGroup}); ObjectDumper.Write(productGroups, 1); }
Result:
All - Simple
This sample uses All to determine whether an array contains only odd numbers.Code:
public void Linq70() { int[] numbers = { 1, 11, 3, 19, 41, 65, 19 }; bool onlyOdd = numbers.All(n => n % 2 == 1); Log.WriteLine("The list contains only odd numbers: {0}", onlyOdd); }
Result:
All - Grouped
This sample uses All to return a grouped a list of products only for categories that have all of their products in stock.Code:
public void Linq72() { Listproducts = GetProductList(); var productGroups = products.GroupBy(prod => prod.Category) .Where(prodGroup => prodGroup.All(p => p.UnitsInStock > 0)) .Select(prodGroup => new {Category = prodGroup.Key, Products = prodGroup}); ObjectDumper.Write(productGroups, 1); }