前言


上一篇《从LINQ开始之LINQ to Objects(上)》主要介绍了LINQ的体系结构、基本语法以及LINQ to Objects中标准查询操作符的使用方法。

本篇则主要讨论LINQ to Objects中的扩展方法以及延迟加载等方面的内容。

扩展方法


扩展方法简介

  扩展方法能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或其他方式修改原始类型。扩展方法是静态方法,它是类的一部分,但实际没有放在类的源代码当中。

下面,我们来看一个简单示例,为上一篇中定义的Employee类添加扩展方法GetSeniority获取员工在本公司的工龄:

public static class EmployeeExtension
{
/// <summary>
/// 计算员工在本公司的工龄
/// </summary>
/// <param name="employee"></param>
/// <returns></returns>
public static long GetSeniority(this Employee employee)
{
TimeSpan ts = DateTime.Now - employee.EntryDate; return (long)ts.TotalDays / 365;
}
}

接下来,遍历employees列表,输出所有员工的姓名及工龄:

 		//获取所有员工的姓名及在本公司的工龄
foreach (var employee in employees)
{
Console.WriteLine("EmployeeName: " + employee.EmployeeName + " Seniority: " + employee.GetSeniority());
} //******************************Output*******************************
//EmployeeName: Mike Seniority: 1
//EmployeeName: Jack Seniority: 10
//EmployeeName: Adolph Seniority: 0
//EmployeeName: Antony Seniority: 6
//EmployeeName: Asa Seniority: 2
//EmployeeName: Bernie Seniority: 9
//EmployeeName: Carl Seniority: 2
//EmployeeName: Duncan Seniority: 7
//EmployeeName: Aimee Seniority: 0
//EmployeeName: Cassie Seniority: 3
//*******************************************************************

由示例可以看出:

1)扩展方法中,可以访问被扩展类型的所有公有方法和属性。

2)第一个参数是要扩展的类型,以this关键字开头。

3)即使扩展方法是静态的,也要使用标准的实例方法语法进行调用。

下面的示例演示了如果扩展方法与类中的某个方法具有相同的签名,则扩展方法不会被调用。在Employee类中定义方法SayHello

 	public void SayHello()
{
Console.WriteLine("Hello , I'm " + EmployeeName);
}

在EmployeeExtension类中为Employee类定义扩展方法SayHello

 	public static void SayHello(this Employee employee)
{
Console.WriteLine("Hello , I'm " + employee.EmployeeName + " ,this is Extension Method");
}

此时,新入职了一位同事Dave,调用SayHello方法向大家问好

 		Employee dave = new Employee("011", "Dave", 30, new DateTime(2017, 5, 25), Sex.Male, Department.PD, 200000, new string[] { "climbing" });
dave.SayHello();
//******************************Output*******************************
//Hello , I'm Dave
//*******************************************************************

注意:此时调用的是Employee类下面的SayHello方法。

使用扩展方法来扩展接口

  把方法扩展到某个接口中,实现该接口的多个类就可以使用相同的实现代码。

以下示例介绍了扩展方法扩展接口的使用场景,首先,定义了一个接口IHobby,接口中包含Play方法

public interface IHobby
{
void Play();
}

分别创建类Reading、Swimming、Shopping实现IHobby接口

public class Reading : IHobby
{
public void Play()
{
Console.WriteLine("I'm Reading.");
}
} public class Swimming : IHobby
{
public void Play()
{
Console.WriteLine("I'm Swimming.");
}
} public class Shopping : IHobby
{
public void Play()
{
Console.WriteLine("I'm Shopping.");
}
}

此时,我们需要在实现IHobby接口的类增加一个的方法ShareFeelings,输出I'm happpy.当然,可以在接口上新增一个方法,然后将实现该接口的类逐个添加ShareFeelings方法,假如实现该接口的类很多,使用扩展方法,就可以大大的减少代码的修改量,测试起来也非常简单。

 	public static void ShareFeelings(this IHobby hobby)
{
Console.WriteLine("I'm happy.");
}

使用接口变量来调用扩展方法

    IHobby hobby = new Reading();
hobby.ShareFeelings();
//******************************Output*******************************
//I'm happy.
//*******************************************************************

LINQ中的扩展方法

  LINQ为IEnumerable<T>接口提供给了各种扩展方法,以便用户在实现了该接口的任意集合上使用LINQ查询。本节主要研究LINQ中Where扩展方法的实现,这个扩展方法位于System.Linq命名空间下的Enumerable类中。

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) {
if (source == null) throw Error.ArgumentNull("source");
if (predicate == null) throw Error.ArgumentNull("predicate");
if (source is Iterator<TSource>) return ((Iterator<TSource>)source).Where(predicate);
if (source is TSource[]) return new WhereArrayIterator<TSource>((TSource[])source, predicate);
if (source is List<TSource>) return new WhereListIterator<TSource>((List<TSource>)source, predicate);
return new WhereEnumerableIterator<TSource>(source, predicate);
}

由上述代码可以看出,Where方法是对IEnumberable接口的扩展,需要传入一个委托参数predicate,该委托要求返回布尔类型。假设我们对List<T>类型的对象调用Where方法,则返回一个WhereListIterator<TSource>对象。WhereListIterator<TSource>类派生自Iterator<TSource>类,下面是Iterator<TSource>类的源码,这里我们只需要注意GetEnumerator方法,该方法对于同一个线程,返回同一个迭代器,不同线程则克隆一个,并将state属性设置为1。

	abstract class Iterator<TSource> : IEnumerable<TSource>, IEnumerator<TSource>
{
int threadId;
internal int state;
internal TSource current; public Iterator() {
threadId = Thread.CurrentThread.ManagedThreadId;
} public TSource Current {
get { return current; }
} public abstract Iterator<TSource> Clone(); public virtual void Dispose() {
current = default(TSource);
state = -1;
} public IEnumerator<TSource> GetEnumerator() {
if (threadId == Thread.CurrentThread.ManagedThreadId && state == 0) {
state = 1;
return this;
}
Iterator<TSource> duplicate = Clone();
duplicate.state = 1;
return duplicate;
} public abstract bool MoveNext(); public abstract IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector); public abstract IEnumerable<TSource> Where(Func<TSource, bool> predicate); object IEnumerator.Current {
get { return Current; }
} IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
} void IEnumerator.Reset() {
throw new NotImplementedException();
}
}

此时,再回到WhereListIterator<TSource>类,该类重写了MoveNext方法。首先,调用GetEnumerator方法获得一个枚举器,在While循环中,只要MoveNext方法返回true,就用Current属性获得集合当前的元素,并使用委托predicate引用的方法处理该元素,返回剩余元素中满足条件的第一个元素。当遍历结束,调用Dispose方法释放非托管资源,并将state属性设置为-1。

	class WhereListIterator<TSource> : Iterator<TSource>
{
List<TSource> source;
Func<TSource, bool> predicate;
List<TSource>.Enumerator enumerator; public WhereListIterator(List<TSource> source, Func<TSource, bool> predicate) {
this.source = source;
this.predicate = predicate;
} public override Iterator<TSource> Clone() {
return new WhereListIterator<TSource>(source, predicate);
} public override bool MoveNext() {
switch (state) {
case 1:
enumerator = source.GetEnumerator();
state = 2;
goto case 2;
case 2:
while (enumerator.MoveNext()) {
TSource item = enumerator.Current;
if (predicate(item)) {
current = item;
return true;
}
}
Dispose();
break;
}
return false;
} public override IEnumerable<TResult> Select<TResult>(Func<TSource, TResult> selector) {
return new WhereSelectListIterator<TSource, TResult>(source, predicate, selector);
} public override IEnumerable<TSource> Where(Func<TSource, bool> predicate) {
return new WhereListIterator<TSource>(source, CombinePredicates(this.predicate, predicate));
}
}

源码传送门:http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,dc4c4c53ff606bc0

延迟加载


延迟执行

  在运行期间定义查询表达式时,查询不会运行,只有在迭代时才进行计算。

下面的示例定义了一个LINQ查询,从集合中找出姓名以A开头的所有员工,因为迭代在查询定义时不会进行,而是在执行每个foreach语句时进行。

		var nameStartWithA = from e in employees
where e.EmployeeName.StartsWith("A")
select e;
Console.WriteLine("First iteration : ");
foreach (var item in nameStartWithA)
{
Console.WriteLine(item.EmployeeName);
} Console.WriteLine(); employees.Add(new Employee("011", "Lily", 25, new DateTime(2017, 5, 29), Sex.Female, Department.HR, 100000, new string[] { "shopping" }));
employees.Add(new Employee("012", "Leo", 28, new DateTime(2017, 5, 29), Sex.Male, Department.IT, 200000, new string[] { "reading" }));
employees.Add(new Employee("013", "Amelia", 29, new DateTime(2017, 5, 29), Sex.Female, Department.PD, 200000, new string[] { "reading", "run" }));
employees.Add(new Employee("014", "Ava", 32, new DateTime(2017, 5, 29), Sex.Female, Department.PD, 400000, new string[] { "swimming" })); Console.WriteLine("Second iteration : ");
foreach (var item in nameStartWithA)
{
Console.WriteLine(item.EmployeeName);
} //******************************Output*******************************
//First iteration :
//Adolph
//Antony
//Asa
//Aimee //Second iteration :
//Adolph
//Antony
//Asa
//Aimee
//Amelia
//Ava
//*******************************************************************

补充:延迟加载的工作原理可从上一章节中对源码的分析得出。

立即执行

  查询在定义表达式时立即执行,而不是在迭代中进行。通过调用ToArray()、ToList()等扩展方法可以实现此项操作。

下面,我们修改上一节中的示例来说明:

		var nameStartWithA = (from e in employees
where e.EmployeeName.StartsWith("A")
select e).ToList();
Console.WriteLine("First iteration : ");
foreach (var item in nameStartWithA)
{
Console.WriteLine(item.EmployeeName);
} Console.WriteLine(); employees.Add(new Employee("011", "Lily", 25, new DateTime(2017, 5, 29), Sex.Female, Department.HR, 100000, new string[] { "shopping" }));
employees.Add(new Employee("012", "Leo", 28, new DateTime(2017, 5, 29), Sex.Male, Department.IT, 200000, new string[] { "reading" }));
employees.Add(new Employee("013", "Amelia", 29, new DateTime(2017, 5, 29), Sex.Female, Department.PD, 200000, new string[] { "reading", "run" }));
employees.Add(new Employee("014", "Ava", 32, new DateTime(2017, 5, 29), Sex.Female, Department.PD, 400000, new string[] { "swimming" })); Console.WriteLine("Second iteration : ");
foreach (var item in nameStartWithA)
{
Console.WriteLine(item.EmployeeName);
} //******************************Output*******************************
//First iteration :
//Adolph
//Antony
//Asa
//Aimee //Second iteration :
//Adolph
//Antony
//Asa
//Aimee
//*******************************************************************

从输出结果中可以看出,两次迭代输出的结果相同,但是集合中值改变了。

示例代码下载:https://github.com/Answer-Geng/LINQ

从LINQ开始之LINQ to Objects(下)的更多相关文章

  1. LINQ简介和LINQ to SQL语句之Where

    LINQ是Language Integrated Query的简称,它是集成在.NET编程语言中的一种特性.已成为编程语言的一个组成部分,在编写程序时可以得到很好的编译时语法检查,丰富的元数据,智能感 ...

  2. LINQ系列:Linq to Object量词操作符

    量词操作符返回一个Boolean值,指示序列中是否存在部分或全部元素符号指定条件.LINQ中量词操作符包括:All.Any和Contains. 1. All All操作符判定在集合中是否所有的值都满足 ...

  3. LINQ系列:LINQ to SQL Exists/In/Any/All/Contains

    1. Any 返回没有Product的Category var expr = from c in context.Categories where !c.Products.Any() select c ...

  4. LINQ / LINQ to SQL / LINQ to XXX 它们到底有什么区别

    LINQ是新生事物,不过从不少文章和讨论上看来,这方面的概念也已经有点混沌不清了.因此我们经常可以看到这样的话: LINQ只能将数据表与实体属性一一对应…… LINQ开发指南:在LINQ中进行数据库字 ...

  5. LINQ to XML LINQ学习第一篇

    LINQ to XML LINQ学习第一篇 1.LINQ to XML类 以下的代码演示了如何使用LINQ to XML来快速创建一个xml: public static void CreateDoc ...

  6. C# LINQ系列:LINQ to DataSet的DataTable操作 及 DataTable与Linq相互转换

    LINQ to DataSet需要使用System.Core.dll.System.Data.dll和System.Data.DataSetExtensions.dll,在项目中添加引用System. ...

  7. Linq实战 之 Linq to Sql及Entity Framework操作详解

    Linq实战 之 Linq to Sql及Entity Framework操作详解 一:linq to db的框架 1. linq to sql 2. linq to ado.net entity f ...

  8. LINQ体验(13)——LINQ to SQL语句之运算符转换和ADO.NET与LINQ to SQL

    运算符转换 1.AsEnumerable:将类型转换为泛型 IEnumerable 使用 AsEnumerable<TSource> 可返回类型化为泛型 IEnumerable 的參数.在 ...

  9. [LINQ2Dapper]最完整Dapper To Linq框架(四)---Linq和SQL并行使用

    目录 [LINQ2Dapper]最完整Dapper To Linq框架(一)---基础查询 [LINQ2Dapper]最完整Dapper To Linq框架(二)---动态化查询 [LINQ2Dapp ...

随机推荐

  1. 怎样配置mysql_installer_community_V5.6........_setup的数据库?

    直接访问查看如下路径就可以根据方法匹配 http://jingyan.baidu.com/album/c35dbcb0f1b1448916fcbcc7.html

  2. Hadoop之HDFS原理及文件上传下载源码分析(下)

    上篇Hadoop之HDFS原理及文件上传下载源码分析(上)楼主主要介绍了hdfs原理及FileSystem的初始化源码解析, Client如何与NameNode建立RPC通信.本篇将继续介绍hdfs文 ...

  3. 【Java 并发】详解 ThreadPoolExecutor

    前言 线程池是并发中一项常用的优化方法,通过对线程复用,减少线程的创建,降低资源消耗,提高程序响应速度.在 Java 中我们一般通过 Exectuors 提供的工厂方法来创建线程池,但是线程池的最终实 ...

  4. 2017/4/25-SAX解析XML文件

    SAX解析XML 1.分析 SAX是按照XML文件的顺序执行,可以说是边扫描,边解析.所以无须将整个文件加载至内存中. 2.优点 1)占用内存少. 2)解析效率高. 3.缺点 1)只能进行读取. 2) ...

  5. python基本数据类型——list

    一.创建列表: li = [] li = list() name_list = ['alex', 'seven', 'eric'] name_list = list(['alex', 'seven', ...

  6. AJAX做增删改查详细!

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. bzoj4785 [Zjoi2017]树状数组

    Description 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的OI 比赛经历.那是一道基础的树状数组题.给出一个长度为 n 的数组 A,初始值都为 0,接下来进 ...

  8. Struts2框架的基本使用(三)

    上篇 Struts2框架的基本使用(二)介绍了Action和result的相关配置操作,本篇接着介绍剩下的异常处理机制和Convention插件的使用.下篇文章介绍的是Struts2框架中标签库的使用 ...

  9. glassfish PWC6351: In TLD scanning 系统找不到指定的文件问题解决

    [2017-04-25T21:26:09.391+0800] [glassfish 4.1] [WARNING] [] [org.apache.jasper.runtime.TldScanner] [ ...

  10. 从零开始构建一个的asp.net Core 项目(二)

    接着上一篇博客继续进行.上一篇博客只是显示了简单的MVC视图页,这篇博客接着进行,连接上数据库,进行简单的CRUD. 首先我在Controllers文件夹点击右键,添加->控制器 弹出的对话框中 ...