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>接口。

最新文章

  1. openswan-ipsec.conf配置说明
  2. Camstar Portal modeling user guid --自定义用户菜单
  3. 来看看Windows9到底是什么
  4. 为什么说基于TCP的移动端IM仍然需要心跳保活?
  5. AC日记——统计数字字符个数 openjudge 1.7 01
  6. JS 之DOM对象(2)
  7. javaNIO(转载)
  8. CentOS安装错误:no default or ui configuration
  9. LaTex 下编译后不能显示中文,或者中文乱码
  10. JS中的call()和apply()方法理解和使用
  11. Explicit Semantic Analysis (ESA)
  12. Kafka生产者-向Kafka中写入数据
  13. BJOI2018 简要题解
  14. 【Jenkins】新版本的特性:自定义流水线
  15. https 适配
  16. Why yarn
  17. 看我怎么扒掉CSDN首页的底裤(python selenium+phantomjs爬取CSDN首页内容)
  18. Fiddler抓包域名过滤
  19. php_Trait
  20. 实现自动文本摘要(python,java)

热门文章

  1. delphi 获取一个字符占用几个字节,方法
  2. 嵌入式&#160;探讨父子线程、进程终止顺序不同产生的结果_skdkjxy_新浪博客
  3. Python学习笔记:一手漂亮的Python函数
  4. 域名解析A记录与CNAME有什么区别?
  5. 安装配置SVN
  6. day9--paramiko模块
  7. 02:实现Singleton模式
  8. QT STUDY
  9. 【定时任务】Spring Boot 中如何使用 Quartz
  10. Framework类库(FCL)简介