首页 > 其他 > 详细

LINQ-进阶的扩展方法

时间:2014-01-16 21:40:07      阅读:415      评论:0      收藏:0      [点我收藏+]

导航

简介

  在LINQ中,不是每一次使用LINQ都要进行一步一步写查询来操作的,完全可以通过.NET自带的方法来简单写LINQ查询语句,这些方法都有一些共同性,其类型实现了 IEnumerable<T> 接口或 IQueryable<T> 接口。IEnumerable&lt;T&gt; or IQueryable&lt;T&gt;.‘ data-guid="e001e287c88bcb58f2b6c589fc55a6e6">此外,许多标准查询运算符方法运行所针对的类型不是基于 IEnumerable<T> 或 IQueryable<T> 的类型。 Enumerable type defines two such methods that both operate on objects of type IEnumerable.‘ data-guid="6275fac4fb94626581bd1792375126e4">Enumerable 类型定义两个此类方法,这些方法都在类型为 IEnumerable 的对象上运行。 Cast&lt;TResult&gt;(IEnumerable) and OfType&lt;TResult&gt;(IEnumerable), let you enable a non-parameterized, or non-generic, collection to be queried in the LINQ pattern.‘ data-guid="7007dc47c61ea0c01bc9102331d224e7">利用这些方法(Cast<TResult>(IEnumerable) 和 OfType<TResult>(IEnumerable)),您将能够在 LINQ 模式中查询非参数化或非泛型集合。 这些方法通过创建一个强类型的对象集合来实现这一点。 Queryable class defines two similar methods, Cast&lt;TResult&gt;(IQueryable) and OfType&lt;TResult&gt;(IQueryable), that operate on objects of type Queryable.‘ data-guid="a4247f9753b25d062ed27e4fc2babab5">Queryable 类定义两个类似的方法(Cast<TResult>(IQueryable) 和 OfType<TResult>(IQueryable)),这些方法在类型为 Queryable 的对象上运行。

 

Enumerable 类的扩展方法

Aggregate:

(1)Enumerable.Aggregate<TSource> 方法 (IEnumerable<TSource>, Func<TSourceTSourceTSource>)方法

    对序列应用累加器函数。

bubuko.com,布布扣
  string sentence = "the quick brown fox jumps over the lazy dog";

            string[] words = sentence.Split( );
            string reversed = words.Aggregate((workingSentence, next) =>
                                                  next + " " + workingSentence );

            //内部执行方式为:
            //1.workingSentence : the      next:quick   结果: quick the
            //2.workingSentence :quick the        next:brown   结果:brown quick the
            //依次累加可以得到结果
bubuko.com,布布扣

(2)Enumerable.Aggregate<TSourceTAccumulate>  (IEnumerable<TSource>, TAccumulate, Func<TAccumulate,TSourceTAccumulate>)方法

    对序列应用累加器函数。 将指定的种子值用作累加器初始值。

bubuko.com,布布扣
            int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };

            // Count the even numbers in the array, using a seed value of 0.
            int numEven = ints.Aggregate(0, (total, next) =>
                                                next % 2 == 0 ? total + 1 : total);

            Console.WriteLine("The number of even integers is: {0}", numEven);
bubuko.com,布布扣

 

 

 

LINQ-进阶的扩展方法

原文:http://www.cnblogs.com/Smilodon/p/3521262.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!