首页 > 代码库 > Linq101-Restriction

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 Simple1() 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 Simple2() 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 Simple3() 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 Simple4() 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 Simple5() 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         /// <summary>101         /// 有一个整数数组,我们找出这个数字是否跟他在这个数组的位置一样 102         /// </summary>103         public void Simple5plus1()104         {105             int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };106          107             // 摘要: 108             //     基于谓词筛选值序列。 将在谓词函数的逻辑中使用每个元素的索引。109             //110             // 参数: 111             //   source:112             //     要筛选的 System.Collections.Generic.IEnumerable<T>。113             //114             //   predicate:115             //     用于测试每个源元素是否满足条件的函数;该函数的第二个参数表示源元素的索引。116             //117             // 类型参数: 118             //   TSource:119             //     source 中的元素的类型。120             //121             // 返回结果: 122             //     一个 System.Collections.Generic.IEnumerable<T>,包含输入序列中满足条件的元素。123             //124             // 异常: 125             //   System.ArgumentNullException:126             //     source 或 predicate 为 null。127             //public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate);128             var numbersInPlace = numbers.Where((n, index) => n == index);129 130             Console.WriteLine("数字跟其位置一样的有:");131             foreach (var number in numbersInPlace)132             {133                 Console.WriteLine(number);134             }135         }136     }137 }

 

Linq101-Restriction