1.需要一个数据源类:

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Linq
  5. {
  6. public class Student
  7. {
  8. public int Id { get; set; }
  9. public string Name { get; set; }
  10. public int Age { get; set; }
  11. }
  12.  
  13. public class Data
  14. {
  15. public static List<Student> studentList = new List<Student>()
  16. {
  17. new Student()
  18. { Name="李四",
  19. Age=
  20. },
  21. new Student()
  22. {
  23. Name="张三",
  24. Age=
  25. }
  26. };
  27.  
  28. }
  29. }

2.主函数调用类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Linq
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. LinqTest lin = new LinqTest();
  14. lin.Show();
  15. }
  16. }
  17. }

3.查询方法(重点介绍的)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Linq
  8. {
  9. public class LinqTest
  10. {
  11. public void Show()
  12. {
  13. List<Student> studentList = new List<Student>();//
  14. Console.WriteLine("-----------------foreach方式(1)---------");
  15.  
  16. foreach (Student item in Data.studentList) ////Data.studentList是数据源
  17. {
  18. if (item.Age > )
  19. {
  20. studentList.Add(item);//将数据源中满足要求的数据填充到指定容器中
  21. }
  22. }
  23.  
  24. //studentList中以填充数据
  25. foreach (Student item in studentList)
  26. {
  27. Console.WriteLine("foreach方式:Name={0},Age={1}", item.Name, item.Age);
  28. }
  29.  
  30. Console.WriteLine("-----------linq方式(2)-------------");
  31. var linq = from s in Data.studentList where s.Age > select s;
  32. foreach (var item in linq)
  33. {
  34. Console.WriteLine("linq方式:Name={0},Age={1}", item.Name, item.Age);
  35. }
  36.  
  37. Console.WriteLine("-----------lambda方式(3)-------------");
  38. // var lambda = Data.studentList.Where(s => s.Age > 18); where可以自动推断类型 扩展方法
  39. var lambda = Data.studentList.Where<Student>(s => s.Age > );
  40. foreach (var item in lambda)
  41. {
  42. Console.WriteLine("lambda方式:Name={0},Age={1}", item.Name, item.Age);
  43. }
  44.  
  45. Console.WriteLine("-------Enumerable静态类方式(4)-----------");
  46. var enm= Enumerable.Where(Data.studentList, s => s.Age > );
  47. foreach (var item in enm)
  48. {
  49. Console.WriteLine("Enumerable静态类方式:Name={0},Age={1}", item.Name, item.Age);
  50. }
  51. }
  52.  
  53. }
  54. }

(3.1)对where扩展方法的理解即学习

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Linq
  8. {
  9. public class LinqTest
  10. {
  11. public void Show()
  12. {
  13. Console.WriteLine("-----------lambda方式(3.1)where扩展-------------");
  14. var linqExtend = Data.studentList.WhereExtend(s => s.Age > ); //where可以自动推断类型 扩展方法
  15.  
  16. foreach (var item in linqExtend)
  17. {
  18. Console.WriteLine("lambda方式(3.1)where扩展:Name={0},Age={1}", item.Name, item.Age);
  19. }
  20. }
  21. }
  22. /// <summary>
  23. /// where的扩展方法
  24. /// </summary>
  25. public static class LinqExtend
  26. {
  27. public static IEnumerable<T> WhereExtend<T>(this IEnumerable<T> tList,Func<T,bool> func) where T : Student// func:用于测试每个元素是否满足条件的函数。
  28. {
  29. var Linq = from s in tList where func(s) select s;
  30. return Linq;
  31. }
  32.  
  33. }
  34. }

几种查询方法(lambda Linq Enumerable静态类方式)的更多相关文章

  1. ZOJ-1610 线段树+两种查询方法(弥补我线段树区间填充的短板)

    ZOJ-1610 线段树+两种查询方法(弥补我线段树区间填充的短板) 题意 题意:给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000 这 ...

  2. MVC EF两种查询方法

    @*@model IQueryable<EFExam.Models.Product>*@@model IQueryable<EFExam.Models.ProductViewMode ...

  3. springdata-jpa 八种查询方法

    使用:maven+Spring+jpa+Junit4 查询方式:SQL,JPQL查询,Specification多条件复杂查询 返回类型:list<POJO>,list<Stinrg ...

  4. Form Builder的三种查询方法构建

    1.使用DEFAULT_WHERE: DECLARE   V_DEFAULT_WHERE VARCHAR2(32767);  V_WHERE         VARCHAR2(32767); BEGI ...

  5. [moka同学笔记]YII2.0 判断签约状态,sql的两种查询方法

    方法一: //判断签约状态 $signed = 0; $sql="SELECT * from usho_community_sign_record WHERE com_id=$r->i ...

  6. Form表单中的三种查询方法

    1.使用:parameter.G_query_find参数: IF (NAME_IN('PO_HEADERS.PO_HEADER_ID') IS NOT NULL) THEN    :paramete ...

  7. Entity Framework入门教程(7)--- EF中的查询方法

    这里主要介绍两种查询方法 Linq to entity(L2E)和Sql 1.L2E查询 L2E查询时可以使用linq query语法,或者lambda表达式,默认返回的类型是IQueryable,( ...

  8. Dynamic CRM 2015学习笔记(3)oData 查询方法及GUID值比较

    本文将比较二种查询字符串在同一个oData查询方法中的不同,另外,还将介绍如何比较不同方法返回的GUID的值. 用同一个oData查询方法,如果传入查询的字符串不一样,返回结果的格式竟然完全不一样. ...

  9. 让LINQ中的查询语法使用自定义的查询方法

    使用LINQ时有两种查询语法:查询语法和方法语法 查询语法:一种类似 SQL 语法的查询方式 方法语法:通过扩展方法和Lambda表达式来创建查询 例如: List<, , , }; //查询语 ...

随机推荐

  1. C#中json字符串的序列化和反序列化

    改文章转自:https://www.cnblogs.com/shang201215019/p/7907655.html 什么是 Json ?        Json[javascript对象表示方法] ...

  2. hdu 3682 10 杭州 现场 C To Be an Dream Architect 容斥 难度:0

    C - To Be an Dream Architect Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  3. 如何在JavaScript中手动创建类数组对象

    前言 关于什么是js的类数组对象这里不再赘述.可以参考这个链接,还有这里. js中类数组对象很多,概念简单的讲就是看上去像数组,又不是数组,可以使用数字下标方式访问又没有数组方法. 例: argume ...

  4. docx文件怎样打开 - 转

    如何打开docx文件?在office2007及2010退出多年后,诸如docx.xlsx.pptx类文件越来越多,我们从网络下载或者别人复制过来的这类文件越来越多.docx文件怎样打开呢?下面有图小站 ...

  5. 201621123006 《Java程序设计》第8周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: 由源代码可 ...

  6. swift 分页视图

    var data:NSArray! var scrollView: UIScrollView! var pageCtrl: UIPageControl! override func viewDidLo ...

  7. HDU3335 Divisibility Dilworth定理+最小路径覆盖

    首先需要一些概念: 有向图,最小路径覆盖,最大独立集,Dilworth,偏序集,跳舞链(DLX).... 理解一: 对于DAG图,有:最大独立集=点-二分匹配数,二分匹配数=最小路径覆盖. 而无向图, ...

  8. Python的安装和使用ubuntu下

    ubuntu下Python的安装和使用 https://www.cnblogs.com/luckyalan/p/6703590.html ubuntu下Python的安装和使用 1 文章介绍 本文介绍 ...

  9. 《DSP using MATLAB》Problem 2.6

    1.代码 %% ------------------------------------------------------------------------ %% Output Info abou ...

  10. 重新理理C++:从《c++ primer》开始

    以前学过C++,但是感觉很多东西还是不清不楚,很多问题解决起来啃吧啃吧的.... 即使c++的东西看过,但是这本书看起来速度还是提不上去,确实需要扎实扎实.很多以前只会用的东西,这本书上都讲的很清楚, ...