几种查询方法(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<, , , }; //查询语 ...
随机推荐
- linux-mint下搭建android,angularjs,rails,html5开发环境
目录[-] 必备软件: 环境配置: [open-jdk-6.0] [android-sdk] [ant] [github] [node.js] [rvm](ruby-1.9.3 rails-4.0.0 ...
- SpringMvc测试框架详解----服务端测试
随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...
- POSIX线程接口编程学习心得
由于实验需要,需要了解下C语言多线程编程的知识,于是学习了下POSIX线程编程的知识,有点心得,记录并分享一下. POSIX(可移植操作系统接口)线程是提高代码响应和性能的有力手段.与标准 fork( ...
- SGU 144. Meeting 概率dp 几何概率分布 难度:0
144. Meeting time limit per test: 0.25 sec. memory limit per test: 4096 KB Two of the three members ...
- HP-UX 下用户被锁
/usr/lbin/getprpw -m lockout root 如果返回lockout=0000000 该帐号是非锁定的,如果是其它的如:lockout=0001000则表示该帐号被锁定 如果是测 ...
- 初始化一个static的Map变量
第一种方法:static块初始化 public class Demo{ private static final Map<String, String> myMap; static { m ...
- 《Drools7.0.0.Final规则引擎教程》番外实例篇——相同对象and List使用
前奏 群组(QQ:593177274)交流中有朋友提出一个问题,怎么实现两个相同对象的插入和比较?相信很多朋友也遇到类似的问题,于是抽时间为大家写一段实例代码,后续代码会同步到GitHub中.下面简单 ...
- Roofline Model与深度学习模型的性能分析
原文链接: https://zhuanlan.zhihu.com/p/34204282 最近在不同的计算平台上验证几种经典深度学习模型的训练和预测性能时,经常遇到模型的实际测试性能表现和自己计算出的复 ...
- DELL E7240超极本
一.图片展示 1.整体图 2.扩展坞的后卡槽附带挡板 3.电源线 4.边框指示灯 5.平躺展示摄像头 二.参数配置 1.i5的 2.i7的 3.手写 处理器:英特尔四代处理器 i7-4600U双 ...
- Codeforces 158B:Taxi
B. Taxi time limit per test 3 seconds memory limit per test 256 megabytes input standard input outpu ...