日期:2014-05-20 浏览次数:21255 次
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)); } } }
(from x in list where x.Length > 4 select x).ToList().ForEach(x => Console.WriteLine(x));
list.Where(x => x.Length > 4 select x).Select(x => x).ToList().ForEach(x => Console.WriteLine(x));
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; } }
if (predicate(item)) yield return item;
if (!predicate(item)) yield return item;
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); } } }