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

libingql發表於2014-10-22

  聚合函式在一系列的值上執行特定的運算,並返回單個值,如在給定元素的值上執行求和或計數運算。
  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();

相關文章