日期:2014-05-20  浏览次数:21206 次

LINQ探秘:实现一个自己的LINQ Provider
C#添加了一项重要的特性,就是 LINQ。

借助LINQ,我们可以用查询语法来检索对象。

C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> list = new List<string>() { "Hello", "World", "ABC", "China" };
            (from x in list where x.Length > 4 select x).ToList().ForEach(x => Console.WriteLine(x));
        }
    }    
}


这样一段代码,可以筛选出字符串列表里面长度大于4的字符。

但是 LINQ 是怎么工作的呢?

其实 LINQ就是调用了 System.Linq 下的一组扩展方法。

C# code
(from x in list where x.Length > 4 select x).ToList().ForEach(x => Console.WriteLine(x));


等价于:
 
C# code
list.Where(x => x.Length > 4 select x).Select(x => x).ToList().ForEach(x => Console.WriteLine(x));


我们注释掉 using System.Linq;。

程序无法编译了。。。

下面我们用自己的代码实现 Where、Select和ToList:

添加如下代码:
C# code
    static class MyLinq
    {
        public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
        {
            foreach (var item in source)
            {
                if (predicate(item)) yield return item;
            }
        }

        public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
        {
            foreach (var item in source)
            {
                yield return selector(item);
            }
        }

        public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
        {
            List<TSource> list = new List<TSource>();
            foreach (var item in source)
                list.Add(item);
            return list;
        }
    }


编译,成功!

但是真的是我们的代码起作用了么?

我们修改下:
C# code
if (predicate(item)) yield return item;

我们修改成
C# code
if (!predicate(item)) yield return item;


再次运行,我们发现过滤反过来了!

赶快改回来。

上面的内容只是热身一下,下面重点来了:

如何实现一个自己的LINQ Provider。

我觉得在讨论这个问题之前,有必要说下为什么我们要实现自己的 Provider。其实很简单:LINQ是基于对象的,有时候我们需要返回来自远程的数据,如果返回所有的数据,再过滤,就浪费了大量的网络带宽。

事实上 Linq to SQL 就是这样——如果没有 Linq To SQL,我们需要从数据库返回所有的数据,然后本地筛选,听听就是多么恐怖的事!!!
Linq To SQL 的原理是,将 LINQ 表达式转换为 SQL,然后再查询和返回数据。

我们的例子围绕这样一个假想的情况展开:我们需要从人人网上获取新鲜事,人人提供了API,我们可以返回某个人的新鲜事。

为了简化起见,例子没有真正从renren获取数据,而是模拟了这个过程。

下面是源程序:

C# code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            (from x in new RenrenFeeds() where x.FeedOwner == "张三" select x).ToList().ForEach(x => Console.WriteLine(x));
        }
    }

    class RenrenFeed
    {
        public string FeedOwner { get; set; }
        public string FeedContent { get; set; }
        public string URL { get; set; }
        public override string ToString()
        {
            return FeedContent;
        }
    }

    class RenrenFeeds : IQueryable<RenrenFeed>, IQueryProvider
    {
        private Expression _expression = null;

        private IList<RenrenFeed> _feeds = new List<RenrenFeed>();

        //在真实的环境里面,我们传入不同的Owner,返回指定的数据
        private IList<RenrenFeed> GetFeeds(string Owner = "")
        {
            List<RenrenFeed> list = new List<RenrenFeed> 
            {
                new RenrenFeed() { FeedOwner = "张三", FeedContent = "张三分享了一张图片", URL = "http://abc.com/photo/zhangsan" },
                new RenrenFeed() { FeedOwner = "张三", FeedContent = "张三撰写了一篇日志:如何使用LINQ", URL = "http://abc.com/blog/zhangsan" },
                new RenrenFeed() { FeedOwner = "李四", FeedContent = "李四分享了一张图片", URL = "http://abc.com/photo/lisi" },
                new RenrenFeed() { FeedOwner = "李四", FeedContent = "李四撰写了一篇日志:C#学习笔记", URL = "http://abc.com/blog/lisi" }
            };
            if (Owner != "")
                return list.Where(x => x.FeedOwner == Owner).ToList();
            else
                return list;
        }

        public IEnumerator<RenrenFeed> GetEnumerator()
        {
            return (this as IQueryable).Provider.Execute<IEnumerator<RenrenFeed>>(_expression);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator<RenrenFeed>)(this as IQueryable).GetEnumerator();
        }

        public Type ElementType
        {
            get { return typeof(RenrenFeed); }
        }

        public Expression Expression
        {
            get { return Expression.Constant(this); }
        }

        public IQueryProvider Provider
        {
            get { return this; }
        }

        public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
        {
            if (typeof(TElement) != typeof(RenrenFeed))
                throw new Exception("不能查询RenrenFeed以外的类型!");

            _expression = expression;

            return (IQueryable<TElement>)this;
        }

        public IQueryable CreateQuery(Expression expression)
        {
            return (IQueryable<RenrenFeed>)(this as IQueryProvider).CreateQuery<RenrenFeed>(expression);
        }

        public TResult Execute<TResult>(Expression expression)
        {
            MethodCallExpression methodcall = _expression as MethodCallExpression;

            //这里没有完成,我们需要解析条件参数,并且转换成对GetFeeds的正确调用。
            foreach (var param in methodcall.Arguments)
            {
                if (param.NodeType == ExpressionType.Quote)
                {
                    LambdaExpression l = Expression.Lambda(Expression.Convert(param, param.Type));
                    LambdaExpression l1 = (LambdaExpression)l.Compile().DynamicInvoke();
                    Func<RenrenFeed, bool> func = (Func<RenrenFeed, bool>)l1.Compile();
                    _feeds = GetFeeds().Where(x => func(x)).ToList();
                }
                Console.WriteLine(param.ToString()); // 我们可以看到 where 产生的 lambda
            }
            return (TResult)_feeds.GetEnumerator();
        }

        public object Execute(Expression expression)
        {
            return (this as IQueryProvider).Execute<IEnumerator<RenrenFeed>>(expression);
        }
    }
}