21扩展IEnumerable<T>泛型接口自定义LINQ的扩展方法
LINQ方法实际上是对IEnumerable<TSource>的扩展,如图:

本篇自定义一个MyWhere方法,达到与Where相同的效果。
使用LINQ自带的Where方法
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>(){1, 2, 3};
IEnumerable<int> query = list.Where(x => x%2 == 0);
list.Add(4);
showConsole(query);
Console.ReadKey();
}
private static void showConsole<T>(IEnumerable<T> list)
{
foreach (T item in list)
{
Console.WriteLine(item.ToString());
}
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果:

这样的结果符合LINQ的"延迟加载"的特点,虽然是在IEnumerable<int> query = list.Where(x => x%2 == 0)之后为集合添加元素list.Add(4),但直到调用showConsole(query)遍历,查询才真正执行。
自定义一个MyWhere,无延迟加载
□ 首先想到的是对IEnumerable<TSource>的扩展,创建静态方法和静态类。
public static class Extension
{
//Func<TSource, bool>是委托,返回的是bool类型
public static IEnumerable<TSource> MyWhere<TSource>(this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
if(source==null) throw new ArgumentException();
if(predicate==null) throw new ArgumentException();
List<TSource> result = new List<TSource>();
foreach (TSource item in source)
{
if (predicate(item))
{
result.Add(item);
}
}
return result;
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
□ 执行主程序
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>(){1, 2, 3};
IEnumerable<int> query = list.MyWhere(x => x % 2 == 0);
list.Add(4);
showConsole(query);
Console.ReadKey();
}
private static void showConsole<T>(IEnumerable<T> list)
{
foreach (T item in list)
{
Console.WriteLine(item.ToString());
}
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
□ 结果

这样的结果丢掉了LINQ的"延迟加载"的特点,也就是后加元素list.Add(4)之后,没有再对集合进行遍历。
可希望的结果是:
● 后加元素list.Add(4)之后,还需要遍历集合
● 返回结果还是IEnumerable<TSource>类型
于是,想到了Decorator设计模式,使用它能满足以上2个条件。
自定义一个MyWhere,也有延迟加载,使用Decorator设计模式
● 为了返回IEnumerable<TSource>类型,必须让装饰者类实现IEnumerable<T>接口
● 装饰者类最重要的特点是包含目标参数类型的引用
● 为了能遍历,装饰者类内部还包含了一个迭代器
public class WhereDecorator<T> : IEnumerable<T>
{
private IEnumerable<T> list;
private Func<T, bool> predicate;
public WhereDecorator(IEnumerable<T> list, Func<T, bool> predicate)
{
this.list = list;
this.predicate = predicate;
}
public IEnumerator<T> GetEnumerator()
{
return new WhereEnumerator<T>(list, predicate);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new WhereEnumerator<T>(list, predicate);
}
public class WhereEnumerator<T> : IEnumerator<T>
{
private List<T> innerList;
private int index;
public WhereEnumerator(IEnumerable<T> list, Func<T, bool> predicate)
{
innerList = new List<T>();
index = -1;
foreach (T item in list)
{
if (predicate(item))
{
innerList.Add(item);
}
}
}
public T Current
{
get { return innerList[index]; }
}
public void Dispose()
{
}
object System.Collections.IEnumerator.Current
{
get { return innerList[index]; }
}
public bool MoveNext()
{
index++;
if (index >= innerList.Count)
{
return false;
}
else
{
return true;
}
}
public void Reset()
{
index = -1;
}
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
自定义MyWhere中,现在可以使用装饰者类来返回一个实例。
public static class Extension
{
public static IEnumerable<TSource> MyWhere<TSource>(this IEnumerable<TSource> source,
Func<TSource, bool> predicate)
{
if(source==null) throw new ArgumentException();
if(predicate==null) throw new ArgumentException();
return new WhereDecorator<TSource>(source, predicate);
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
主程序中:
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>(){1, 2, 3};
IEnumerable<int> query = list.MyWhere(x => x % 2 == 0);
list.Add(4);
showConsole(query);
Console.ReadKey();
}
private static void showConsole<T>(IEnumerable<T> list)
{
foreach (T item in list)
{
Console.WriteLine(item.ToString());
}
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
结果:

可见,与LINQ的Where方法返回结果一样。
总结
● 所有的LINQ方法是对IEnumerable<T>的扩展
● 当我们想对方法返回的结果再进行链式操作的时候,装饰者类就包含方法参数类型的引用并返回与该方法相同的类型。
● 这里的装饰者类需要完成遍历,装饰者类必须实现IEnumerable<T>,内部必须存在迭代器实现IEnumerator<T>接口。
21扩展IEnumerable<T>泛型接口自定义LINQ的扩展方法的更多相关文章
- .NET中扩展方法和Enumerable(System.Linq)
LINQ是我最喜欢的功能之一,程序中到处是data.Where(x=x>5).Select(x)等等的代码,她使代码看起来更好,更容易编写,使用起来也超级方便,foreach使循环更加容易,而不 ...
- C# IEnumerable与IQueryable ,IEnumerable与IList ,LINQ理解Var和IEnumerable
原文:https://www.cnblogs.com/WinHEC/articles/understanding-var-and-ienumerable-with-linq.html 使用LINQ从数 ...
- IEnumerable和IQueryable和Linq的查询
IEnumerable和IEnumerable 1.IEnumerable查询必须在本地执行.并且执行查询前我们必须把所有的数据加载到本地.而且更多的时候.加载的数据有大量的数据是我们不需要的无效数据 ...
- atitit. 集合groupby 的实现(2)---自定义linq查询--java .net php
atitit. 集合groupby 的实现(2)---自定义linq查询--java .net php 实现方式有如下 1. Linq的实现原理流程(ati总结) 1 2. groupby 与 事 ...
- Linq的Distinct方法的扩展
原文地址:如何很好的使用Linq的Distinct方法 Person1: Id=1, Name="Test1" Person2: Id=1, Name="Test1&qu ...
- ReactiveX 学习笔记(11)对 LINQ 的扩展
Interactive Extensions(Ix) 本文的主题为对 Ix 库,对 LINQ 的扩展. Buffer Ix.NET Buffer Ix.NET BufferTest Buffer 方法 ...
- NHibernate Linq查询 扩展增强 (第九篇)
在上一篇的Linq to NHibernate的介绍当中,全部是namespace NHibernate命名空间中的IQueryOver<TRoot, TSubType>接口提供的.IQu ...
- 【C#夯实】我与接口二三事:IEnumerable、IQueryable 与 LINQ
序 学生时期,有过小组作业,当时分工一人做那么两三个页面,然而在前端差不多的时候,我和另一个同学发生了争执.当时用的是简单的三层架构(DLL.BLL.UI),我个人觉得各写各的吧,到时候合并,而他觉得 ...
- 记录一次源码扩展案列——FastJson自定义反序列化ValueMutator
背景:曾经遇到一个很麻烦的事情,就是一个json串中有很多占位符,需要替换成特定文案.如果将json转换成对象后,在一个一个属性去转换的话就出出现很多冗余代码,不美观也不是很实用. 而且也不能提前在j ...
随机推荐
- python目录/文件操作
目录操作 sys.argv[0] # 获得当前脚本路径,即当前工作目录\脚本名 os.getcwd() # 获得当前工作目录 os.path.abspath('.') # 获得当前工作目录 os.pa ...
- spark集群安装[转]
[转]http://sofar.blog.51cto.com/353572/1352713 ====================================================== ...
- Ninject中如果在抽象类中使用了属性注入,则属性必须设置为protected或public
Ninject中如果在抽象类中使用了属性注入,则属性必须设置为protected或public 不能使用private,否则无法注入成功,会报null异常
- HTML小工具
一般可能用的到的符号代码: 符号 HTML 符号 HTML & & < < > > ⁄ ⁄ " " ¸ ¸ ° ° ½ ½ ¼ ¼ ...
- CCF CSP 201412-4 最优灌溉
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201412-4 最优灌溉 问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖 ...
- 【LOJ】#2105. 「TJOI2015」概率论
题解 可以说是什么找规律好题了 但是要推生成函数,非常神奇-- 任何的一切都可以用\(n^2\)dp说起 我们所求即是 所有树的叶子总数/所有树的方案数 我们可以列出一个递推式,设\(g(x)\)为\ ...
- PHP问题解决
1.PHP在上传文件的时候出现错误Internal Server Error Internal Server ErrorThe server encountered an internal error ...
- win7下docker环境搭建nginx+php-fpm+easyswoole开发环境
基础的环境已在文章nginx.php-fpm.swoole HTTP/TCP压测对比中搭建了,现在是在这个基础上在搭建easyswoole开发环境 主要要修改的地方是dnmp包里面的docker-co ...
- [CodeForces]CodeForces - 1025F Disjoint Triangles
题意: 给出平面上n个点,问能在其中选出6个点,组成两个三角形,使得其互不相交 问有多少种选法 大致思路 考虑枚举一条直线,将所有得点分为左右两部分,其中有两个点在直线上, 以这两个点为顶点,分别统 ...
- Linux 的文件权限与目录配置
用户和用户组 文件所有者 (owner) 用户组概念 (group) 其他人概念 (others) Linux文件权限概念 1. Linux文件属性 要了解Linux文件属性,那么有个重要的命令必须提 ...