Method Description Skip 跳过序列中指定数量元素,然后返回剩余序列 SkipWhile 只要满足条件,就跳过序列中的元素,然后返回剩余函数 Take 从序列的开头返回指定数量的连续元素 TakeWhile 只要满足条件,就返回元素 IList<string> strList = new List<string>(){ "One", "Two", "Three", "Four", &…
表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构变得透明清楚, Expression<Func<Student, && s.age < ; 编译器将上面的表达式翻译成下面的表达式树 Expression.Lambda<Func<Student, bool>>( Expression.AndAlso(…
Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 ThenBy 第二级升序排序,仅在方法查询中使用 ThenByDescending 第二级降序排序,仅在方法查询中使用 Reverse 反转集合,仅在方法查询中使用 IList<Student> studentList = new List<Student>() { , StudentName…
延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现延迟加载 public static class EnumerableExtensionMethods { public static IEnumerable<Student> GetTeenAgerStudents(this IEnumerable<Student> source)…
Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join Join操作两个集合,inner collection 和 outer collection 它返回一个集合(包含两个集合根据特定条件结合的所有元素),和SQL中的inner join一样 public static IEnumerable<TResult> Join<TOuter, TIn…
1.where Filtering Operators Description Where Returns values from the collection based on a predicate function OfType Returns values from the collection based on a specified type. However, it will depend on their ability to cast to a specified type.…
Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 ToLookup ToLookup is the same as GroupBy; the only difference is the execution of GroupBy is deferred whereas ToLookup execution is immediate. IList<Stude…
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } , , StudentName = } , , StudentName = } , , StudentName = } }; var lowercaseStudentNames = from s in studentList where s.StudentName.ToLower().StartsWith(…
聚合操作执行数学的运算,如平均数.合计.总数.最大值.最小值 Method Description Aggregate 在集合上执行自定义聚集操作 Average 求平均数 Count 求集合的总数 LongCount 求集合的总数 Max 最大值 Min 最小值 Sum 总数 public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TS…
IList<Student> studentList = new List<Student>() { , StudentName = "John" }, , StudentName = "Moin" }, , StudentName = "Bill" }, , StudentName = "Ram" }, , StudentName = "Ron" } }; var selectRe…
OfType操作根据集合中的元素是否是给定的类型进行筛选 IList mixedList = new ArrayList(); mixedList.Add(); mixedList.Add("One"); mixedList.Add("Two"); mixedList.Add(); mixedList.Add(, StudentName = "Bill" }); var stringResult = from s in mixedList.OfT…
Method Description AsEnumerable Returns the input sequence as IEnumerable<t> AsQueryable Converts IEnumerable to IQueryable, to simulate a remote query provider Cast Coverts a non-generic collection to a generic collection (IEnumerable to IEnumerabl…
Set Operators Usage Distinct 去掉集合的重复项 Except 返回两个集合的不同,第一个集合的元素不能出现在第二个集合中 Intersect 返回两个集合的交集,即元素同时出现在两个集合中 Union Returns unique elements from two sequences, which means unique elements that appear in either of the two sequences. IList<string> strL…
Element Operators (Methods) Description ElementAt 返回指定索引的元素,如果索引超过集合长度,则抛出异常 ElementAtOrDefault 返回指定索引的元素,如果索引超过集合长度,则返回元素的默认值,不抛出异常 First 返回集合的第一个元素,可以根据指定条件返回 FirstOrDefault 返回集合的第一个元素,可以根据指定条件返回,如果集合不存在元素,则返回元素的默认值 Last 返回集合中的最后一个元素,可以根据条件返回 LastO…
IList<, , }; var avg = intList.Average(); Console.WriteLine("Average: {0}", avg); IList<Student> studentList = new List<Student>>() { , StudentName = } , , StudentName = } , , StudentName = } , , StudentName = } , , StudentName…
Operator Description All 判断所有的元素是否满足条件 Any 判断存在一个元素满足条件 Contain 判断是否包含元素 IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } , , StudentName = } , , StudentName = } , , StudentName = } }; // checks whether…
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } , , StudentName = } , , StudentName = } , , StudentName = }, , StudentName = } }; var thenByResult = studentList.OrderBy(s => s.StudentName).ThenBy(s =…
1.查询语法 Query Syntax: from <range variable> in <IEnumerable<T> or IQueryable<T> Collection> <Standard Query Operators> <lambda expression> <select or groupBy operator> <result formation> // string collection…
IList<Student> studentList = new List<Student>() { , StudentName = , StandardID = } , , StudentName = , StandardID = } , , StudentName = , StandardID = } , , StudentName = , StandardID = } , , StudentName = } }; IList<Standard> standardL…
Linq分区操作之Skip,SkipWhile,Take,TakeWhile源码分析 二:linq的分区操作 常用的分区操作:Take,TakeWhile,Skip,SkipWhile 三:Take 1. 注释: 从序列的开头返回指定数量的连续元素 2. 实战: var nums = new int[] { 10, 20, 30, 40, 50, 60 }; var query = nums.Take(2).ToList(); // 10,20 3. 探究源码: 四:TakeWhile 1. 注…
前面我们主要讲解的是Linq的查询表达式,Linq不但提供了一些基本的查询表达式,还提供了数十个查询操作.比如筛选操作.聚合操作.投影操作等等.通过这些查询操作可以更方便的对数据源进行处理. Linq提供了数十个查询操作,大多数的操作都是针对实现了IQueryable<T>和IEnumerbale<T>接口的序列. 序号     查询操作           对应的查询表达式                 说明                                   …
一.Skip()跳过 static void Main(string[] args) { //skip()跳过 ,,,,,,,,,}; //跳过3条 nums.Skip().ToList().ForEach(i=>Console.WriteLine(i)); } 二.take()提取 static void Main(string[] args) { //take()提取 ,,,,,,,,,}; //提取3条 nums.Take().ToList().ForEach(i=>Console.Wr…
Enumerable: Queryable:…
LINQ(Language Integrated Query) LINQ语言集成查询是一组用于C#语言的扩展.它允许编写C#代码对数据集进行查询,比如查询内存中的对象或查询远程数据库的表.利用linq,程序员不必掌握数据库查询语句而是使用Linq就能完成相同的查询任务.而传统数据查询的弱点很多,比如执行简单查询也需要冗长的操作代码,查询语句是字符串格式,无法让编译器执行检查错误及早提示,查询不是强类型,查询参数容易写错,查询结果没有真正面向对象,每次查询取结果还得事先知道列名或列索引,不使用抽象…
LINQ标准的查询操作符 首先我们来看一下LINQ的操作符,可根据查询操作符的操作”类型”进行分类,如把它们分成投影,限制,排序,联接,分组,串联,聚合,集合,生成,转换,元素,相等,量词,分割等. 类型 操作符名称 投影操作符 Select,SelectMany 限制操作符 Where 排序操作符 OrderBy,OrderByDescending,ThenBy,ThenByDescending,Reverse 联接操作符 Join,GroupJoin 分组操作符 GroupBy 串联操作符…
本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5832322.html,记录一下学习过程以备后续查用. LINQ 简介:     语言集成查询(LINQ)是Visual Studio 2008和.NET Framework 3.5版中引入的一项创新功能. 传统上,针对数据的查询都是以简单的字符串表示,而没有编译时类型检查或IntelliSense支持.此外,您还必须针对以下各种数据源学习一种不同的查询 语言:SQL数据库.XML文档.各种Web服务等.通过…
本笔记摘抄自:https://www.cnblogs.com/liqingwen/p/5801249.html,记录一下学习过程以备后续查用. “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法,大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了IEnumerable<T>接口 或 IQueryable<T> 接口.标准查询运算符提供了包括筛选.投影.聚合.排序等功能在内的查询功能,各个标准查询运算符在执行时间上有所不同,具体情况 取决于它们是返回单一值还…
LINQ 标准查询操作概述 序 “标准查询运算符”是组成语言集成查询 (LINQ) 模式的方法.大多数这些方法都在序列上运行,其中的序列是一个对象,其类型实现了IEnumerable<T> 接口或 IQueryable<T> 接口.标准查询运算符提供了包括筛选.投影.聚合.排序等功能在内的查询功能.        各个标准查询运算符在执行时间上有所不同,具体情况取决于它们是返回单一值还是值序列.返回单一值的方法(例如 Average 和 Sum)会立即执行.返回序列的方法会延迟查询…
一.投影操作符 1. Select Select操作符对单个序列或集合中的值进行投影.下面的示例中使用select从序列中返回Employee表的所有列: using (NorthwindDataContext db=new NorthwindDataContext()) { //查询语法 var query = from e in db.Employees where e.FirstName.StartsWith("M") select e; //方法语法 var q = db.Em…
一.投影操作符 1. Select Select操作符对单个序列或集合中的值进行投影.下面的示例中使用select从序列中返回Employee表的所有列: using (NorthwindDataContext db=new NorthwindDataContext()) { //查询语法 var query = from e in db.Employees where e.FirstName.StartsWith("M") select e; //方法语法 var q = db.Em…