• 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 }
  • 相关阅读:
    Learning Intents behind Interactions with Knowledge Graph for Recommendation
    php_network_getaddresses: getaddrinfo failed: Name or service not known
    下载低版本Xcode方法
    世界奇妙周刊 第2期
    倒计时 | 7.24 阿里云 Serverless Developer Meetup 杭州站报名火热进行中!
    《网镜》001.生成word勘验报告
    java 常用集合使用方法
    Excel 常用操作
    《世界上最简单的会计书》
    mysql 通过坐标换算距离
  • 原文地址:https://www.cnblogs.com/David-Huang/p/4126640.html
Copyright © 2020-2023  润新知