几种查询方法(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<, , , }; //查询语 ...
随机推荐
- 关于UDP很好的书籍和文章(整理、持续更新)
文章 告知你不为人知的 UDP:疑难杂症和使用(必看)
- Docker和k8s的区别与介绍
本文来源:鲜枣课堂 2010年,几个搞IT的年轻人,在美国旧金山成立了一家名叫“dotCloud”的公司. 这家公司主要提供基于PaaS的云计算技术服务.具体来说,是和LXC有关的容器技术. LXC, ...
- 从invalidAuthenticationToken❌想到需要忽略:verify_authenticity_token。
https://www.cnblogs.com/chentianwei/diary/2018/07/11/9296126.html 这篇日记记录了我遇到的invalidAuthenticaitonTo ...
- Apereo CAS - 1
1. download cas 4.2.2 from https://github.com/apereo/cas/releases 2. eclipse import cas 4.2.2 eclip ...
- linux shell 按行循环读入文件方法
转http://blog.csdn.net/hittata/article/details/7042779 #/bin/bash printf "******************** ...
- phalcon 设置cookie一直是httponly导致前端读取不到cookie的值
解决办法: 修改配置如果不好使,则暂时降低phalcon版本为3.1.2. 注意设置cookie的参数secure的值为false,否则js还是读取不到cookie
- JS在项目中用到的AOP, 以及函数节流, 防抖, 事件总线
1. 项目中在绑定事件的时候总想在触发前,或者触发后做一些统一的判断或逻辑,在c#后端代码里,可以用Attribute, filter等标签特性实现AOP的效果,可是js中没有这种用法,归根到本质还是 ...
- 新书《Cocos2dx 3.x 3D图形学渲染技术讲解》问世
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D实战核心技术详解 ...
- 仿智能社官网:原生JS实现简单又酷炫的3D立方体时钟
先放一下我做的效果:https://linrunzheng.github.io/3Dclock/3Dclock/new.html 至于3D立方体怎么做这里就不在阐述了,可以看一下我之前的博客. 这里默 ...
- MySQL 练习题4
1.表结构如下: #课程表 CREATE TABLE `course` ( `c_id` ) NOT NULL, `c_name` ) DEFAULT NULL, `t_id` ) DEFAULT N ...