LINQ - Restriction Operators
View Code
1 Program.cs 19.66KB 2 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.ComponentModel; 8 using System.IO; 9 using System.Xml.Linq; 10 11 namespace RestrictionOperators 12 { 13 class Program 14 { 15 static void Main(string[] args) 16 { 17 LinqSamples samples = new LinqSamples(); 18 19 //Comment or uncomment the method calls below to run or not 20 21 samples.Linq1(); // This sample uses the where clause to find all elements of an array with a value 22 // less than 5 23 24 //samples.Linq2(); // This sample uses the where clause to find all products that are out of stock 25 26 //samples.Linq3(); // This sample uses the where clause to find all products that are in stock and cost 27 // more than 3.00 per unit 28 29 //samples.Linq4(); // This sample uses the where clause to find all customers in Washington and then it 30 // uses a foreach loop to iterate over the orders collection that belongs to each 31 // customer 32 33 //samples.Linq5(); // This sample demonstrates an indexed where clause that returns digits whose name is 34 // shorter than their value 35 } 36 37 public class Product 38 { 39 public int ProductID { get; set; } 40 public string ProductName { get; set; } 41 public string Category { get; set; } 42 public decimal UnitPrice { get; set; } 43 public int UnitsInStock { get; set; } 44 } 45 46 public class Order 47 { 48 public int OrderID { get; set; } 49 public DateTime OrderDate { get; set; } 50 public decimal Total { get; set; } 51 } 52 53 public class Customer 54 { 55 public string CustomerID { get; set; } 56 public string CompanyName { get; set; } 57 public string Address { get; set; } 58 public string City { get; set; } 59 public string Region { get; set; } 60 public string PostalCode { get; set; } 61 public string Country { get; set; } 62 public string Phone { get; set; } 63 public string Fax { get; set; } 64 public Order[] Orders { get; set; } 65 } 66 67 public class LinqSamples 68 { 69 private List<Product> productList; 70 private List<Customer> customerList; 71 72 [Description("This sample uses the where clause to find all elements of an array with a value less than 5.")] 73 public void Linq1() 74 { 75 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 76 77 var lowNums = 78 from num in numbers 79 where num < 5 80 select num; 81 82 Console.WriteLine("Numbers < 5:"); 83 foreach (var x in lowNums) 84 { 85 Console.WriteLine(x); 86 } 87 } 88 89 [Description("This sample uses the where clause to find all products that are out of stock.")] 90 public void Linq2() 91 { 92 List<Product> products = GetProductList(); 93 94 var soldOutProducts = 95 from prod in products 96 where prod.UnitsInStock == 0 97 select prod; 98 99 Console.WriteLine("Sold out products:"); 100 foreach (var product in soldOutProducts) 101 { 102 Console.WriteLine("{0} is sold out!", product.ProductName); 103 } 104 } 105 106 [Description("This sample uses the where clause to find all products that are in stock and " + 107 "cost more than 3.00 per unit.")] 108 public void Linq3() 109 { 110 List<Product> products = GetProductList(); 111 112 var expensiveInStockProducts = 113 from prod in products 114 where prod.UnitsInStock > 0 && prod.UnitPrice > 3.00M 115 select prod; 116 117 Console.WriteLine("In-stock products that cost more than 3.00:"); 118 foreach (var product in expensiveInStockProducts) 119 { 120 Console.WriteLine("{0} is in stock and costs more than 3.00.", product.ProductName); 121 } 122 } 123 124 [Description("This sample uses the where clause to find all customers in Washington " + 125 "and then it uses a foreach loop to iterate over the orders collection that belongs to each customer.")] 126 public void Linq4() 127 { 128 List<Customer> customers = GetCustomerList(); 129 130 var waCustomers = 131 from cust in customers 132 where cust.Region == "WA" 133 select cust; 134 135 Console.WriteLine("Customers from Washington and their orders:"); 136 foreach (var customer in waCustomers) 137 { 138 Console.WriteLine("Customer {0}: {1}", customer.CustomerID, customer.CompanyName); 139 foreach (var order in customer.Orders) 140 { 141 Console.WriteLine(" Order {0}: {1}", order.OrderID, order.OrderDate); 142 } 143 } 144 } 145 146 [Description("This sample demonstrates an indexed where clause that returns digits whose name is " + 147 "shorter than their value.")] 148 public void Linq5() 149 { 150 string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; 151 152 var shortDigits = digits.Where((digit, index) => digit.Length < index); 153 154 Console.WriteLine("Short digits:"); 155 foreach (var d in shortDigits) 156 { 157 Console.WriteLine("The word {0} is shorter than its value.", d); 158 } 159 } 160 161 public List<Product> GetProductList() 162 { 163 if (productList == null) 164 createLists(); 165 166 return productList; 167 } 168 169 public List<Customer> GetCustomerList() 170 { 171 if (customerList == null) 172 createLists(); 173 174 return customerList; 175 } 176 177 private void createLists() 178 { 179 // Product data created in-memory using collection initializer: 180 productList = 181 new List<Product> { 182 new Product { ProductID = 1, ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 39 }, 183 new Product { ProductID = 2, ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = 17 }, 184 new Product { ProductID = 3, ProductName = "Aniseed Syrup", Category = "Condiments", UnitPrice = 10.0000M, UnitsInStock = 13 }, 185 new Product { ProductID = 4, ProductName = "Chef Anton's Cajun Seasoning", Category = "Condiments", UnitPrice = 22.0000M, UnitsInStock = 53 }, 186 new Product { ProductID = 5, ProductName = "Chef Anton's Gumbo Mix", Category = "Condiments", UnitPrice = 21.3500M, UnitsInStock = 0 }, 187 new Product { ProductID = 6, ProductName = "Grandma's Boysenberry Spread", Category = "Condiments", UnitPrice = 25.0000M, UnitsInStock = 120 }, 188 new Product { ProductID = 7, ProductName = "Uncle Bob's Organic Dried Pears", Category = "Produce", UnitPrice = 30.0000M, UnitsInStock = 15 }, 189 new Product { ProductID = 8, ProductName = "Northwoods Cranberry Sauce", Category = "Condiments", UnitPrice = 40.0000M, UnitsInStock = 6 }, 190 new Product { ProductID = 9, ProductName = "Mishi Kobe Niku", Category = "Meat/Poultry", UnitPrice = 97.0000M, UnitsInStock = 29 }, 191 new Product { ProductID = 10, ProductName = "Ikura", Category = "Seafood", UnitPrice = 31.0000M, UnitsInStock = 31 }, 192 new Product { ProductID = 11, ProductName = "Queso Cabrales", Category = "Dairy Products", UnitPrice = 21.0000M, UnitsInStock = 22 }, 193 new Product { ProductID = 12, ProductName = "Queso Manchego La Pastora", Category = "Dairy Products", UnitPrice = 38.0000M, UnitsInStock = 86 }, 194 new Product { ProductID = 13, ProductName = "Konbu", Category = "Seafood", UnitPrice = 6.0000M, UnitsInStock = 24 }, 195 new Product { ProductID = 14, ProductName = "Tofu", Category = "Produce", UnitPrice = 23.2500M, UnitsInStock = 35 }, 196 new Product { ProductID = 15, ProductName = "Genen Shouyu", Category = "Condiments", UnitPrice = 15.5000M, UnitsInStock = 39 }, 197 new Product { ProductID = 16, ProductName = "Pavlova", Category = "Confections", UnitPrice = 17.4500M, UnitsInStock = 29 }, 198 new Product { ProductID = 17, ProductName = "Alice Mutton", Category = "Meat/Poultry", UnitPrice = 39.0000M, UnitsInStock = 0 }, 199 new Product { ProductID = 18, ProductName = "Carnarvon Tigers", Category = "Seafood", UnitPrice = 62.5000M, UnitsInStock = 42 }, 200 new Product { ProductID = 19, ProductName = "Teatime Chocolate Biscuits", Category = "Confections", UnitPrice = 9.2000M, UnitsInStock = 25 }, 201 new Product { ProductID = 20, ProductName = "Sir Rodney's Marmalade", Category = "Confections", UnitPrice = 81.0000M, UnitsInStock = 40 }, 202 new Product { ProductID = 21, ProductName = "Sir Rodney's Scones", Category = "Confections", UnitPrice = 10.0000M, UnitsInStock = 3 }, 203 new Product { ProductID = 22, ProductName = "Gustaf's Knäckebröd", Category = "Grains/Cereals", UnitPrice = 21.0000M, UnitsInStock = 104 }, 204 new Product { ProductID = 23, ProductName = "Tunnbröd", Category = "Grains/Cereals", UnitPrice = 9.0000M, UnitsInStock = 61 }, 205 new Product { ProductID = 24, ProductName = "Guaraná Fantástica", Category = "Beverages", UnitPrice = 4.5000M, UnitsInStock = 20 }, 206 new Product { ProductID = 25, ProductName = "NuNuCa Nuß-Nougat-Creme", Category = "Confections", UnitPrice = 14.0000M, UnitsInStock = 76 }, 207 new Product { ProductID = 26, ProductName = "Gumbär Gummibärchen", Category = "Confections", UnitPrice = 31.2300M, UnitsInStock = 15 }, 208 new Product { ProductID = 27, ProductName = "Schoggi Schokolade", Category = "Confections", UnitPrice = 43.9000M, UnitsInStock = 49 }, 209 new Product { ProductID = 28, ProductName = "Rössle Sauerkraut", Category = "Produce", UnitPrice = 45.6000M, UnitsInStock = 26 }, 210 new Product { ProductID = 29, ProductName = "Thüringer Rostbratwurst", Category = "Meat/Poultry", UnitPrice = 123.7900M, UnitsInStock = 0 }, 211 new Product { ProductID = 30, ProductName = "Nord-Ost Matjeshering", Category = "Seafood", UnitPrice = 25.8900M, UnitsInStock = 10 }, 212 new Product { ProductID = 31, ProductName = "Gorgonzola Telino", Category = "Dairy Products", UnitPrice = 12.5000M, UnitsInStock = 0 }, 213 new Product { ProductID = 32, ProductName = "Mascarpone Fabioli", Category = "Dairy Products", UnitPrice = 32.0000M, UnitsInStock = 9 }, 214 new Product { ProductID = 33, ProductName = "Geitost", Category = "Dairy Products", UnitPrice = 2.5000M, UnitsInStock = 112 }, 215 new Product { ProductID = 34, ProductName = "Sasquatch Ale", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 111 }, 216 new Product { ProductID = 35, ProductName = "Steeleye Stout", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 20 }, 217 new Product { ProductID = 36, ProductName = "Inlagd Sill", Category = "Seafood", UnitPrice = 19.0000M, UnitsInStock = 112 }, 218 new Product { ProductID = 37, ProductName = "Gravad lax", Category = "Seafood", UnitPrice = 26.0000M, UnitsInStock = 11 }, 219 new Product { ProductID = 38, ProductName = "Côte de Blaye", Category = "Beverages", UnitPrice = 263.5000M, UnitsInStock = 17 }, 220 new Product { ProductID = 39, ProductName = "Chartreuse verte", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 69 }, 221 new Product { ProductID = 40, ProductName = "Boston Crab Meat", Category = "Seafood", UnitPrice = 18.4000M, UnitsInStock = 123 }, 222 new Product { ProductID = 41, ProductName = "Jack's New England Clam Chowder", Category = "Seafood", UnitPrice = 9.6500M, UnitsInStock = 85 }, 223 new Product { ProductID = 42, ProductName = "Singaporean Hokkien Fried Mee", Category = "Grains/Cereals", UnitPrice = 14.0000M, UnitsInStock = 26 }, 224 new Product { ProductID = 43, ProductName = "Ipoh Coffee", Category = "Beverages", UnitPrice = 46.0000M, UnitsInStock = 17 }, 225 new Product { ProductID = 44, ProductName = "Gula Malacca", Category = "Condiments", UnitPrice = 19.4500M, UnitsInStock = 27 }, 226 new Product { ProductID = 45, ProductName = "Rogede sild", Category = "Seafood", UnitPrice = 9.5000M, UnitsInStock = 5 }, 227 new Product { ProductID = 46, ProductName = "Spegesild", Category = "Seafood", UnitPrice = 12.0000M, UnitsInStock = 95 }, 228 new Product { ProductID = 47, ProductName = "Zaanse koeken", Category = "Confections", UnitPrice = 9.5000M, UnitsInStock = 36 }, 229 new Product { ProductID = 48, ProductName = "Chocolade", Category = "Confections", UnitPrice = 12.7500M, UnitsInStock = 15 }, 230 new Product { ProductID = 49, ProductName = "Maxilaku", Category = "Confections", UnitPrice = 20.0000M, UnitsInStock = 10 }, 231 new Product { ProductID = 50, ProductName = "Valkoinen suklaa", Category = "Confections", UnitPrice = 16.2500M, UnitsInStock = 65 }, 232 new Product { ProductID = 51, ProductName = "Manjimup Dried Apples", Category = "Produce", UnitPrice = 53.0000M, UnitsInStock = 20 }, 233 new Product { ProductID = 52, ProductName = "Filo Mix", Category = "Grains/Cereals", UnitPrice = 7.0000M, UnitsInStock = 38 }, 234 new Product { ProductID = 53, ProductName = "Perth Pasties", Category = "Meat/Poultry", UnitPrice = 32.8000M, UnitsInStock = 0 }, 235 new Product { ProductID = 54, ProductName = "Tourtière", Category = "Meat/Poultry", UnitPrice = 7.4500M, UnitsInStock = 21 }, 236 new Product { ProductID = 55, ProductName = "Pâté chinois", Category = "Meat/Poultry", UnitPrice = 24.0000M, UnitsInStock = 115 }, 237 new Product { ProductID = 56, ProductName = "Gnocchi di nonna Alice", Category = "Grains/Cereals", UnitPrice = 38.0000M, UnitsInStock = 21 }, 238 new Product { ProductID = 57, ProductName = "Ravioli Angelo", Category = "Grains/Cereals", UnitPrice = 19.5000M, UnitsInStock = 36 }, 239 new Product { ProductID = 58, ProductName = "Escargots de Bourgogne", Category = "Seafood", UnitPrice = 13.2500M, UnitsInStock = 62 }, 240 new Product { ProductID = 59, ProductName = "Raclette Courdavault", Category = "Dairy Products", UnitPrice = 55.0000M, UnitsInStock = 79 }, 241 new Product { ProductID = 60, ProductName = "Camembert Pierrot", Category = "Dairy Products", UnitPrice = 34.0000M, UnitsInStock = 19 }, 242 new Product { ProductID = 61, ProductName = "Sirop d'érable", Category = "Condiments", UnitPrice = 28.5000M, UnitsInStock = 113 }, 243 new Product { ProductID = 62, ProductName = "Tarte au sucre", Category = "Confections", UnitPrice = 49.3000M, UnitsInStock = 17 }, 244 new Product { ProductID = 63, ProductName = "Vegie-spread", Category = "Condiments", UnitPrice = 43.9000M, UnitsInStock = 24 }, 245 new Product { ProductID = 64, ProductName = "Wimmers gute Semmelknödel", Category = "Grains/Cereals", UnitPrice = 33.2500M, UnitsInStock = 22 }, 246 new Product { ProductID = 65, ProductName = "Louisiana Fiery Hot Pepper Sauce", Category = "Condiments", UnitPrice = 21.0500M, UnitsInStock = 76 }, 247 new Product { ProductID = 66, ProductName = "Louisiana Hot Spiced Okra", Category = "Condiments", UnitPrice = 17.0000M, UnitsInStock = 4 }, 248 new Product { ProductID = 67, ProductName = "Laughing Lumberjack Lager", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 52 }, 249 new Product { ProductID = 68, ProductName = "Scottish Longbreads", Category = "Confections", UnitPrice = 12.5000M, UnitsInStock = 6 }, 250 new Product { ProductID = 69, ProductName = "Gudbrandsdalsost", Category = "Dairy Products", UnitPrice = 36.0000M, UnitsInStock = 26 }, 251 new Product { ProductID = 70, ProductName = "Outback Lager", Category = "Beverages", UnitPrice = 15.0000M, UnitsInStock = 15 }, 252 new Product { ProductID = 71, ProductName = "Flotemysost", Category = "Dairy Products", UnitPrice = 21.5000M, UnitsInStock = 26 }, 253 new Product { ProductID = 72, ProductName = "Mozzarella di Giovanni", Category = "Dairy Products", UnitPrice = 34.8000M, UnitsInStock = 14 }, 254 new Product { ProductID = 73, ProductName = "Röd Kaviar", Category = "Seafood", UnitPrice = 15.0000M, UnitsInStock = 101 }, 255 new Product { ProductID = 74, ProductName = "Longlife Tofu", Category = "Produce", UnitPrice = 10.0000M, UnitsInStock = 4 }, 256 new Product { ProductID = 75, ProductName = "Rhönbräu Klosterbier", Category = "Beverages", UnitPrice = 7.7500M, UnitsInStock = 125 }, 257 new Product { ProductID = 76, ProductName = "Lakkalikööri", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 57 }, 258 new Product { ProductID = 77, ProductName = "Original Frankfurter grüne Soße", Category = "Condiments", UnitPrice = 13.0000M, UnitsInStock = 32 } 259 }; 260 261 // Customer/Order data read into memory from XML file using XLinq: 262 customerList = ( 263 from e in XDocument.Load("Customers.xml"). 264 Root.Elements("customer") 265 select new Customer 266 { 267 CustomerID = (string)e.Element("id"), 268 CompanyName = (string)e.Element("name"), 269 Address = (string)e.Element("address"), 270 City = (string)e.Element("city"), 271 Region = (string)e.Element("region"), 272 PostalCode = (string)e.Element("postalcode"), 273 Country = (string)e.Element("country"), 274 Phone = (string)e.Element("phone"), 275 Fax = (string)e.Element("fax"), 276 Orders = ( 277 from o in e.Elements("orders").Elements("order") 278 select new Order 279 { 280 OrderID = (int)o.Element("id"), 281 OrderDate = (DateTime)o.Element("orderdate"), 282 Total = (decimal)o.Element("total") 283 }) 284 .ToArray() 285 }) 286 .ToList(); 287 } 288 } 289 } 290 }
101 LINQ Samples
Learn how to use LINQ in your applications with these code samples, covering the entire range of LINQ functionality and demonstrating LINQ with SQL, DataSets, and XML.
http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b/viewsamplepack
作者:backslash112 (美国CS研究生在读/机器人工程师)
出处:http://sirkevin.cnblogs.com
GitHub:https://github.com/backslash112
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://sirkevin.cnblogs.com
GitHub:https://github.com/backslash112
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2012-04-24 14:28 backslash112 阅读(182) 评论(0) 编辑 收藏 举报