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 ...
随机推荐
- Java OOM学习
转载自原文: 什么是java OOM?如何分析及解决oom问题? 什么是OOM? OOM,全称"Out Of Memory",翻译成中文就是"内存用完了",表现 ...
- 20155225 实验三《敏捷开发与XP实践》实验报告
20155225 实验三<敏捷开发与XP实践>实验报告 一.使用工具(Code->Reformate Code)把代码重新格式化 IDEA里的Code菜单有很多实用的功能可以帮助我们 ...
- C语言:输入10个整数,找出其中绝对值最小的数
1 输入10个整数,找出其中绝对值最小的数(10分) 题目描述 输入10个整数,找出其中绝对值最小的数 输入 十个整数 输出 绝对值最小的数 样例输入 -10 -2 30 40 50 60 70 80 ...
- 程序设计实习MOOC / 程序设计与算法(二)第二周测验(2018春季)
递归算法: 1:全排列 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列. 我们假设对于小写字母有'a' < ' ...
- 【LOJ】#2064. 「HAOI2016」找相同字符
题解 做后缀自动机题要一点脑洞,脑洞一开,就过了 我们显然要拿第二个串跑第一个串的后缀自动机 我们可以求出第二个串每个位置匹配到的节点,和匹配的长度L 那么我们统计一个后缀树上的根缀和,表示这样个节点 ...
- PHP 中文字符串相关
1.字符串长度 中文字符串使用strlen() 获取字符串长度时一个UTF8的中文字符是3个字节长度:使用mb_strlen(),选定内码为UTF8,则会将一个中文字符当作长度1来计算 在对含中文字符 ...
- 【Java】java.lang.NullPointerException的两个原因
自己写程序时出现过 java.lang.NullPointerException错误的一些原因 (1)自己建立了一个数组,但每个数组元素没单独初始化(单独new一个新对象). (2)在进行if (ar ...
- 使用IDEA和Maven创建Javaweb项目
1.File -- New -- Project
- Picasso:开启大前端的未来
“道生一,一生二,二生三,三生万物.” —— <道德经> Picasso是大众点评移动研发团队自研的高性能跨平台动态化框架,经过两年多的孕育和发展,目前在美团多个事业群已经实现了大规模的应 ...
- [leetcode DP]63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...