LINQ系列:Linq to Object聚合操作符

  聚合函数在一系列的值上执行特定的运算,并返回单个值,如在给定元素的值上执行求和或计数运算。
  LINQ共有7种聚合操作符:Aggregate、Average、Count、LongCount、Max、Min和Sum。

1. Aggregate

  Aggregate操作符从某一特定序列或集合中收集值。当聚合完成时,它将序列中返回的值进行累积并返回结果。

1>. 原型定义

public static TSource Aggregate<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func);

2>. 示例

var expr = context.Categories
    .Select(c => c.CategoryName)
    .ToArray()
    .Aggregate((current, next) => String.Format("{0}-{1}", current, next));

Console.WriteLine(expr);

2. Average

  Average操作符计算一个数值序列的平均值。

1>.原型定义

public static double Average(this IEnumerable<double> source);
public static double Average<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector);

2>.示例

int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
Console.WriteLine(fibonacci.Average());
var expr = context.Products
    .Average(p => p.UnitPrice);

Console.WriteLine(expr);
var expr = context.Products
    .Select(p=>p.UnitPrice)
    .Average();
var expr = from p in context.Products
           select p;

expr.Average(p => p.UnitPrice)
var expr = from p in context.Products
           select p.UnitPrice;
expr.Average();

3. Count

  Count操作符计算一个特定集合中元素的个数。

1>. 原型定义

public static int Count<TSource>(this IQueryable<TSource> source);
public static int Count<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate);

2>. 示例

int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
Console.WriteLine(fibonacci.Count());
var expr = context.Products.Count();
var expr = context.Products
    .Where(p => p.UnitPrice > 10m)
    .Count();
var expr = context.Products
    .Count(p => p.UnitPrice > 10m);
int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
var expr = from f in fibonacci
            where f > 5
            select f;
expr.Count();
var expr = from c in context.Categories
           select new
           {
               c.CategoryName,
               ProductsCount = c.Products.Count()
           };

4. LongCount

  LongCount操作符会返回一个Int64类型的值,用来计算元素数大于Int32.MaxValue的集合的元素数。LongCount的使用方法与Count相同。

1>. 原型定义

public static long LongCount<TSource>(this IQueryable<TSource> source);
public static long LongCount<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate);

2>. 示例

var expr = context.Products
    .LongCount(p => p.UnitPrice > 10m);

5. Max

  Max操作符返回一个序列中的最大值。

1>. 原型定义

public static TSource Max<TSource>(this IQueryable<TSource> source);
public static TResult Max<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector);

2>. 示例

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int maxNumber = numbers.Max();
var expr = from p in context.Products
            select p;
expr.Max(p => p.UnitPrice > 10m);
var expr = from p in context.Products
            select p.UnitPrice;
expr.Max();
var expr = context.Products
    .Max(p => p.UnitPrice > 10m);
var expr = context.Products
    .Select(p => p.UnitPrice)
    .Max();
IEnumerable<Product> query = from p in context.Products
                             where p.UnitPrice == context.Products.Max(product => product.UnitPrice)
                             select p;
IEnumerable<Product> query = from p in context.Products
                             where p.UnitPrice == context.Products.OrderByDescending(product => product.UnitPrice).First().UnitPrice
                             select p;

6. Min

  Min操作符返回一个序列中的最小值。

1>. 原型定义

public static TSource Min<TSource>(this IQueryable<TSource> source);
public static TResult Min<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector);

2>. 示例

string[] words = { "cherry", "apple", "blueberry" };
int shortestWord = words.Min(w => w.Length);
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int minNumber = numbers.Min();
var expr = context.Products
    .Select(p => p.UnitPrice)
    .Min();

7. Sum

  Sum操作符计算集合中选定值的总和。

1>. 原型定义

public static int Sum(this IQueryable<int> source);
public static int Sum<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, int>> selector);

2>. 示例

int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
var expr = from f in fibonacci
            select f;
expr.Sum();
int[] fibonacci = new int[] { 1, 1, 2, 3, 5, 8, 13, 21 };
var expr = from f in fibonacci
            where f > 5
            select f;
expr.Sum();
var expr = from p in context.Products
           select p;
expr.Sum(p => p.UnitPrice);
var expr = from p in context.Products
           select p.UnitPrice;
expr.Sum();
posted @ 2014-10-22 11:05  libingql  阅读(2239)  评论(0编辑  收藏  举报