几种查询方法(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<, , , }; //查询语 ...
随机推荐
- [图床神器]Windows下的图片上传工具MPic
最近用hexo在github上搭建了一个静态博客,开始几天用起来感觉还挺好的,但是用了些天就觉得每次写文章插入图片就非常麻烦,而且如果图片多了的话上传和访问就很慢了.后来网上看了下发现mac下有款ip ...
- UVA-140 Bandwidth (回溯+剪枝)
题目大意:求一个使带宽最小的排列和最小带宽.带宽是指一个字母到其相邻字母的距离最大值. 题目分析:在递归生成全排列的过程中剪枝,剪枝方案还是两个.一.当前解不如最优解优时,减去:二.预测的理想解不必最 ...
- https ddos检测——研究现状
from: https://jyx.jyu.fi/bitstream/handle/123456789/52275/1/URN%3ANBN%3Afi%3Ajyu-201612125051.pdf 相关 ...
- 浅谈jsonp
要谈jsonp,首先要弄明白jsonp是什么,它是用来干嘛的.jsonp其实就是我们常用的script标签,用来解决跨域的,只不过这个标签是动态创建的,为啥要动态创建涅. 举个小栗子: 假如我们远程文 ...
- Cookie是什么?从哪来?存在哪?往哪去?
什么是cookie? cookie最简单的介绍就是服务器返回的一个字符串信息,只不过我们每次请求都需要把它发送给服务器.以AFN和android-async-http为例子,默认都会把cookie自动 ...
- hdu 6154 CaoHaha's staff
CaoHaha's staff Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 剑指offer+名企面试官精讲典型编程题,28题扩展题
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- LINUX中的RCU机制的分析
RCU机制是Linux2.6之后提供的一种数据一致性访问的机制,从RCU(read-copy-update)的名称上看,我们就能对他的实现机制有一个大概的了解,在修改数据的时候,首先需要读取数据,然后 ...
- 《转》深入理解Activity启动流程(三)–Activity启动的详细流程2
本文原创作者:Cloud Chou. 出处:本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 深入理解Activity启动流程(一)--Activity启 ...
- HDU 2807
http://acm.hdu.edu.cn/showproblem.php?pid=2807 把矩阵相乘放在第二重循环,第三重循环只进行比较可以水过,优化的方法不懂 主要用这题练习floyd的写法 # ...