Linq101-Restriction

  1 using System;
  2 using System.Linq;
  3 
  4 namespace Linq101
  5 {
  6     class Restriction
  7     {
  8         /// <summary>
  9         /// This sample uses where to find all elements of an array less than 5.
 10         /// </summary>
 11         public void Linq1()
 12         {
 13             int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
 14 
 15             var query = from n in numbers
 16                         where n < 5
 17                         select n;
 18 
 19             Console.WriteLine("Numbers < 5 :");
 20             foreach (var number in query)
 21             {
 22                 Console.WriteLine(number);
 23             }
 24         }
 25 
 26         /// <summary>
 27         /// This sample uses where to find all products that are out of stock.
 28         /// </summary>
 29         public void Linq2()
 30         {
 31             var productList = Data.GetProductList();
 32 
 33             var query = from product in productList
 34                         where product.UnitsInStock == 0
 35                         select product;
 36 
 37             Console.WriteLine("Sold out products:");
 38             foreach (var product in query)
 39             {
 40                 Console.WriteLine("{0} is sold out!", product.ProductName);
 41             }
 42         }
 43 
 44         /// <summary>
 45         /// This sample uses where to find all products that are in stock and cost more than 3.00 per unit.
 46         /// </summary>
 47         public void Linq3()
 48         {
 49             var productList = Data.GetProductList();
 50 
 51             var query = from product in productList
 52                         where product.UnitsInStock > 0 && product.UnitPrice > 3.00M
 53                         select product;
 54 
 55             Console.WriteLine("In-stock products that cost more than 3.00:");
 56             foreach (var product in query)
 57             {
 58                 Console.WriteLine("{0} is in stock and cost more than 3.00", product.ProductName);
 59             }
 60         }
 61 
 62         /// <summary>
 63         /// This sample uses where to find all customers in Washington and then uses the resulting sequence to drill down into their orders.
 64         /// </summary>
 65         public void Linq4()
 66         {
 67             var customerList = Data.GetCustomerList();
 68 
 69             var query = from customer in customerList
 70                         where customer.Region == "WA"
 71                         select customer;
 72 
 73             Console.WriteLine("Cutomers from Washington and their orders:");
 74             foreach (var customer in query)
 75             {
 76                 Console.WriteLine("Customer {0}:{1}", customer.CustomerID, customer.CompanyName);
 77                 foreach (var order in customer.Orders)
 78                 {
 79                     Console.WriteLine("    Order {0}:{1}", order.OrderID, order.OrderDate);
 80                 }
 81             }
 82         }
 83 
 84         /// <summary>
 85         /// This sample demonstrates an indexed Where clause that returns digits whose name is shorter than their value.
 86         /// </summary>
 87         public void Linq5()
 88         {
 89             string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
 90 
 91             var query = digits.Where((digit, index) => digit.Length < index);
 92 
 93             Console.WriteLine("Short digits:");
 94             foreach (var digit in query)
 95             {
 96                 Console.WriteLine("The word {0} is shorter than its value.", digit);
 97             }
 98         }
99 } 100 }
原文地址:https://www.cnblogs.com/David-Huang/p/4126640.html