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 ==
  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[] == '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 == );
  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 = { , , , , , , , , , };
  65.  
  66. int number = (from n in numbers
  67. where n >
  68. select n).ElementAt();
  69.  
  70. Console.WriteLine("Second number > 5: {0}", number);
  71. }
  72. }
  73. }

Linq101-Element的更多相关文章

  1. Spring配置文件标签报错:The prefix "XXX" for element "XXX:XXX" is not bound. .

    例如:The prefix "context" for element "context:annotation-config" is not bound. 这种 ...

  2. 【解决方案】cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'. One of '{"http://java.sun.com/xml/ns/javaee":run-as, "http://java.sun.com/xml/ns/javaee":security-role-r

    [JAVA错误] cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'. One o ...

  3. WebComponent魔法堂:深究Custom Element 之 从过去看现在

    前言  说起Custom Element那必然会想起那个相似而又以失败告终的HTML Component.HTML Component是在IE5开始引入的新技术,用于对原生元素作功能"增强& ...

  4. WebComponent魔法堂:深究Custom Element 之 标准构建

    前言  通过<WebComponent魔法堂:深究Custom Element 之 面向痛点编程>,我们明白到其实Custom Element并不是什么新东西,我们甚至可以在IE5.5上定 ...

  5. WebComponent魔法堂:深究Custom Element 之 面向痛点编程

    前言  最近加入到新项目组负责前端技术预研和选型,一直偏向于以Polymer为代表的WebComponent技术线,于是查阅各类资料想说服老大向这方面靠,最后得到的结果是:"资料99%是英语 ...

  6. 深入理解DOM节点类型第五篇——元素节点Element

    × 目录 [1]特征 [2]子节点 [3]特性操作[4]attributes 前面的话 元素节点Element非常常用,是DOM文档树的主要节点:元素节点是html标签元素的DOM化结果.元素节点主要 ...

  7. cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mvc:annotation-driven'.

    spring 配置文件报错报错信息:cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be ...

  8. MongoDB查询转对象是出错Element '_id' does not match any field or property of class

    MongoDB查询转对象是出错Element '_id' does not match any field or property of class   解决方法: 1.在实体类加:[BsonIgno ...

  9. [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素

    Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...

  10. [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

随机推荐

  1. [BZOJ 1816] [Cqoi2010] 扑克牌 【二分答案】

    题目链接:BZOJ - 1816 题目分析 答案具有可以二分的性质,所以可以二分答案. 验证一个答案 x 是否可行,就累加一下各种牌相对于 x 还缺少的量,如果总和超过了 x 或 m ,就不可行. 因 ...

  2. [BZOJ 2243] [SDOI 2011] 染色 【树链剖分】

    题目链接:BZOJ - 2243 题目分析 树链剖分...写了200+行...Debug了整整一天+... 静态读代码读了 5 遍 ,没发现错误,自己做小数据也过了. 提交之后全 WA . ————— ...

  3. java打jar包 命令行cmd在当前路径打jar包

    不尝试就永远不会知道真相. 今天搞webservice,需要将服务单独拉出来发布.打jar包的时候要打成aar包,所以用到cmd下的打jar包的命令. 当前路径打jar包,一定要先进到这个文件夹,然后 ...

  4. GCD - Extreme (II)

    uva11424: 题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gc ...

  5. visualvm监控jvm及远程jvm监控方法(转)

    VisualVM是Sun的一个OpenJDK项目,其目的在于为Java应用创建一个整套的问题解决工具.它集成了多个JDK命令工具的一个可视化工具,它主要用来监控JVM的运行情况,可以用它来查看和浏览H ...

  6. BZOJ1611: [Usaco2008 Feb]Meteor Shower流星雨

    1611: [Usaco2008 Feb]Meteor Shower流星雨 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 904  Solved: 393 ...

  7. Linux Shell编程(6)——变量替换

    变量的名字是它的值保存的地方.引用它的值称为变量替换.$让我们仔细地区别变量和变量的值.如果variable1是一个变量的名字,那么$variable1就是引用这个变量的值――即这个变量它包含的数据. ...

  8. selenuim ide回放时出现的问题

    [error] Unexpected Exception: fileName -> chrome://selenium-ide/content/selenium-core/scripts/htm ...

  9. 什么是:VGA SVGA XGA SXGA

    经常可以看到VGA这些专业术语,通常这些重要技术指标指的是液晶屏(TFT LCD)的分辨率. TFT是英文Thin Film Transistor的缩写,中文意思是薄膜晶体管. VGA(Video G ...

  10. python数据的存储和持久化操作

    Python的数据持久化操作主要是六类:普通文件.DBM文件.Pickled对象存储.shelve对象存储.对象数据库存储.关系数据库存储. 普通文件不解释了,DBM就是把字符串的键值对存储在文件里: ...