几种查询方法(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<, , , }; //查询语 ...
随机推荐
- [Checking for libstdc++-4.4.4-13.el6-i686; Not found. Failed] 的解决。
单纯 yum install libstdc++-4.4.4.i686 是不行的. 应该安装 yum install libstdc++-devel.i686 顺带就能装上需要的lib 真够变态的. ...
- org.springframework.transaction 包改成 spring-tx
org.springframework.transaction 包改成 spring-tx org.springframework.transaction 3.2.2以后的版本,全改到 spring ...
- 豆知识扩展:HTML<meta> tag
豆知识: HTML<meta> tag Metadata 是关于数据的信息. The <meta> tag provides metadata关于网页.Metadat不会显示在 ...
- hdu1564博弈+找规律
#include<map> #include<set> #include<cmath> #include<queue> #include<stac ...
- OAF 设置右对齐
public void setAllColsFormat(OAPageContext paramOAPageContext, OAWebBean paramOAWebBean) { OATableBe ...
- Bitdefender Total Security 2014 Free 6 Months & 12 month License Key
German Only – Bitdefender Total Security 2014 Free 6 Months Serial License Keyhttp://www.bitdefender ...
- EM算法及其应用: K-means 与 高斯混合模型
EM算法及其应用(一) EM算法及其应用(二): K-means 与 高斯混合模型 上一篇阐述了EM算法的主要原理,这一篇来看其两大应用 -- K-means 与 高斯混合模型,主要由EM算法的观点出 ...
- L201
The American public’s obsession with dieting has led to one of the most dangerous healthmisconceptio ...
- Kotlin Reference (十) Interfaces
most from reference 接口 Kotlin中的接口非常类似于Java8,它们可以包含抽象方法的声明以及方法实现.与抽象类不同的是接口不能存储状态.它们可以具有属性,但这些需要是抽象的或 ...
- HDU 2485
http://acm.hdu.edu.cn/showproblem.php?pid=2485 n个车站,m条边,两边之间费用为1,问最少摧毁多少车站,使得1-n无法在k时间内到达 将2-(n-1)每个 ...