几种查询方法(lambda Linq Enumerable静态类方式)
1.需要一个数据源类:
- using System;
- using System.Collections.Generic;
- namespace Linq
- {
- public class Student
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public int Age { get; set; }
- }
- public class Data
- {
- public static List<Student> studentList = new List<Student>()
- {
- new Student()
- { Name="李四",
- Age=
- },
- new Student()
- {
- Name="张三",
- Age=
- }
- };
- }
- }
2.主函数调用类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Linq
- {
- class Program
- {
- static void Main(string[] args)
- {
- LinqTest lin = new LinqTest();
- lin.Show();
- }
- }
- }
3.查询方法(重点介绍的)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Linq
- {
- public class LinqTest
- {
- public void Show()
- {
- List<Student> studentList = new List<Student>();//
- Console.WriteLine("-----------------foreach方式(1)---------");
- foreach (Student item in Data.studentList) ////Data.studentList是数据源
- {
- if (item.Age > )
- {
- studentList.Add(item);//将数据源中满足要求的数据填充到指定容器中
- }
- }
- //studentList中以填充数据
- foreach (Student item in studentList)
- {
- Console.WriteLine("foreach方式:Name={0},Age={1}", item.Name, item.Age);
- }
- Console.WriteLine("-----------linq方式(2)-------------");
- var linq = from s in Data.studentList where s.Age > select s;
- foreach (var item in linq)
- {
- Console.WriteLine("linq方式:Name={0},Age={1}", item.Name, item.Age);
- }
- Console.WriteLine("-----------lambda方式(3)-------------");
- // var lambda = Data.studentList.Where(s => s.Age > 18); where可以自动推断类型 扩展方法
- var lambda = Data.studentList.Where<Student>(s => s.Age > );
- foreach (var item in lambda)
- {
- Console.WriteLine("lambda方式:Name={0},Age={1}", item.Name, item.Age);
- }
- Console.WriteLine("-------Enumerable静态类方式(4)-----------");
- var enm= Enumerable.Where(Data.studentList, s => s.Age > );
- foreach (var item in enm)
- {
- Console.WriteLine("Enumerable静态类方式:Name={0},Age={1}", item.Name, item.Age);
- }
- }
- }
- }
(3.1)对where扩展方法的理解即学习
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Linq
- {
- public class LinqTest
- {
- public void Show()
- {
- Console.WriteLine("-----------lambda方式(3.1)where扩展-------------");
- var linqExtend = Data.studentList.WhereExtend(s => s.Age > ); //where可以自动推断类型 扩展方法
- foreach (var item in linqExtend)
- {
- Console.WriteLine("lambda方式(3.1)where扩展:Name={0},Age={1}", item.Name, item.Age);
- }
- }
- }
- /// <summary>
- /// where的扩展方法
- /// </summary>
- public static class LinqExtend
- {
- public static IEnumerable<T> WhereExtend<T>(this IEnumerable<T> tList,Func<T,bool> func) where T : Student// func:用于测试每个元素是否满足条件的函数。
- {
- var Linq = from s in tList where func(s) select s;
- return Linq;
- }
- }
- }
几种查询方法(lambda Linq Enumerable静态类方式)的更多相关文章
- ZOJ-1610 线段树+两种查询方法(弥补我线段树区间填充的短板)
ZOJ-1610 线段树+两种查询方法(弥补我线段树区间填充的短板) 题意 题意:给一个n,代表n次操作,接下来每次操作表示把[l,r]区间的线段涂成k的颜色其中,l,r,k的范围都是0到8000 这 ...
- MVC EF两种查询方法
@*@model IQueryable<EFExam.Models.Product>*@@model IQueryable<EFExam.Models.ProductViewMode ...
- springdata-jpa 八种查询方法
使用:maven+Spring+jpa+Junit4 查询方式:SQL,JPQL查询,Specification多条件复杂查询 返回类型:list<POJO>,list<Stinrg ...
- Form Builder的三种查询方法构建
1.使用DEFAULT_WHERE: DECLARE V_DEFAULT_WHERE VARCHAR2(32767); V_WHERE VARCHAR2(32767); BEGI ...
- [moka同学笔记]YII2.0 判断签约状态,sql的两种查询方法
方法一: //判断签约状态 $signed = 0; $sql="SELECT * from usho_community_sign_record WHERE com_id=$r->i ...
- Form表单中的三种查询方法
1.使用:parameter.G_query_find参数: IF (NAME_IN('PO_HEADERS.PO_HEADER_ID') IS NOT NULL) THEN :paramete ...
- Entity Framework入门教程(7)--- EF中的查询方法
这里主要介绍两种查询方法 Linq to entity(L2E)和Sql 1.L2E查询 L2E查询时可以使用linq query语法,或者lambda表达式,默认返回的类型是IQueryable,( ...
- Dynamic CRM 2015学习笔记(3)oData 查询方法及GUID值比较
本文将比较二种查询字符串在同一个oData查询方法中的不同,另外,还将介绍如何比较不同方法返回的GUID的值. 用同一个oData查询方法,如果传入查询的字符串不一样,返回结果的格式竟然完全不一样. ...
- 让LINQ中的查询语法使用自定义的查询方法
使用LINQ时有两种查询语法:查询语法和方法语法 查询语法:一种类似 SQL 语法的查询方式 方法语法:通过扩展方法和Lambda表达式来创建查询 例如: List<, , , }; //查询语 ...
随机推荐
- C#中json字符串的序列化和反序列化
改文章转自:https://www.cnblogs.com/shang201215019/p/7907655.html 什么是 Json ? Json[javascript对象表示方法] ...
- 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 ...
- 如何在JavaScript中手动创建类数组对象
前言 关于什么是js的类数组对象这里不再赘述.可以参考这个链接,还有这里. js中类数组对象很多,概念简单的讲就是看上去像数组,又不是数组,可以使用数字下标方式访问又没有数组方法. 例: argume ...
- docx文件怎样打开 - 转
如何打开docx文件?在office2007及2010退出多年后,诸如docx.xlsx.pptx类文件越来越多,我们从网络下载或者别人复制过来的这类文件越来越多.docx文件怎样打开呢?下面有图小站 ...
- 201621123006 《Java程序设计》第8周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码如下: 由源代码可 ...
- swift 分页视图
var data:NSArray! var scrollView: UIScrollView! var pageCtrl: UIPageControl! override func viewDidLo ...
- HDU3335 Divisibility Dilworth定理+最小路径覆盖
首先需要一些概念: 有向图,最小路径覆盖,最大独立集,Dilworth,偏序集,跳舞链(DLX).... 理解一: 对于DAG图,有:最大独立集=点-二分匹配数,二分匹配数=最小路径覆盖. 而无向图, ...
- Python的安装和使用ubuntu下
ubuntu下Python的安装和使用 https://www.cnblogs.com/luckyalan/p/6703590.html ubuntu下Python的安装和使用 1 文章介绍 本文介绍 ...
- 《DSP using MATLAB》Problem 2.6
1.代码 %% ------------------------------------------------------------------------ %% Output Info abou ...
- 重新理理C++:从《c++ primer》开始
以前学过C++,但是感觉很多东西还是不清不楚,很多问题解决起来啃吧啃吧的.... 即使c++的东西看过,但是这本书看起来速度还是提不上去,确实需要扎实扎实.很多以前只会用的东西,这本书上都讲的很清楚, ...