zoukankan      html  css  js  c++  java
  • Linq101-Element

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 
     5 namespace Linq101
     6 {
     7     class Element
     8     {
     9         /// <summary>
    10         /// This sample uses First to return the first matching element as a Product, instead of as a sequence containing a Product.
    11         /// </summary>
    12         public void Linq58()
    13         {
    14             List<Data.Product> products = Data.GetProductList();
    15 
    16             Data.Product product12 = (from p in products
    17                                       where p.ProductID == 12
    18                                       select p).First();
    19 
    20             ObjectDumper.Write(product12);
    21         }
    22 
    23         /// <summary>
    24         /// This sample uses First to find the first element in the array that starts with 'o'.
    25         /// </summary>
    26         public void Linq59()
    27         {
    28             string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
    29 
    30             string startsWithO = strings.First(s => s[0] == 'o');
    31 
    32             Console.WriteLine("A string starting with 'o': {0}", startsWithO);
    33         }
    34 
    35         /// <summary>
    36         /// This sample uses FirstOrDefault to try to return the first element of the sequence, unless there are no elements, in which case the default value for that type is returned.
    37         /// </summary>
    38         public void Linq60()
    39         {
    40             int[] numbers = { };
    41 
    42             int firstNumOrDefault = numbers.FirstOrDefault();
    43 
    44             Console.WriteLine(firstNumOrDefault);
    45         }
    46 
    47         /// <summary>
    48         /// This sample uses FirstOrDefault to return the first product whose ProductID is 789 as a single Product object, unless there is no match, in which case null is returned.
    49         /// </summary>
    50         public void Linq61()
    51         {
    52             List<Data.Product> products = Data.GetProductList();
    53 
    54             Data.Product product789 = products.FirstOrDefault(p => p.ProductID == 789);
    55 
    56             Console.WriteLine("Product 789 exists: {0}", product789 != null);
    57         }
    58 
    59         /// <summary>
    60         /// This sample uses ElementAt to retrieve the second number greater than 5 from an array.
    61         /// </summary>
    62         public void Linq62()
    63         {
    64             int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
    65 
    66             int number = (from n in numbers
    67                           where n > 5
    68                           select n).ElementAt(1);
    69 
    70             Console.WriteLine("Second number > 5: {0}", number);
    71         }
    72     }
    73 }
  • 相关阅读:
    codevs 1792 分解质因数
    ScrollView嵌套ViewPager,ViewPager内容不显示问题
    AndroidStudio不重新运行,Debug调试已有进程
    Android进阶之Fragment与Activity之间的数据交互
    Android修改包名的方法,简单粗暴。
    android studio 按钮运行按钮后,不弹出选择运行模拟器的对话框
    RGB颜色值与十六进制颜色码转换工具
    Android Studio查看其它APP的布局结构
    android实现多条件筛选列表菜单筛选菜单
    Android,一条线串联实心圆布局
  • 原文地址:https://www.cnblogs.com/David-Huang/p/4323687.html
Copyright © 2011-2022 走看看