聚合查询

聚合查询

Chloe 可以像写 sql 一样实现聚合查询。

IQuery<Person> q = dbContext.Query<Person>();

q.Select(a => Sql.Count()).First();
/*
 * SELECT TOP (1) COUNT(1) AS [C] FROM [Person] AS [Person]
 */

/* 支持多个聚合函数 */
q.Select(a => new
{
    Count = Sql.Count(),
    LongCount = Sql.LongCount(),
    Sum = Sql.Sum(a.Age),
    Max = Sql.Max(a.Age),
    Min = Sql.Min(a.Age),
    Average = Sql.Average(a.Age)
}).First();
/*
 * SELECT TOP (1) COUNT(1) AS [Count],COUNT_BIG(1) AS [LongCount],CAST(SUM([Person].[Age]) AS INT) AS [Sum],MAX([Person].[Age]) AS [Max],MIN([Person].[Age]) AS [Min],CAST(AVG([Person].[Age]) AS FLOAT) AS [Average] 
   FROM [Person] AS [Person]
 */

var count = q.Count();
/*
 * SELECT COUNT(1) AS [C] FROM [Person] AS [Person]
 */

var longCount = q.LongCount();
/*
 * SELECT COUNT_BIG(1) AS [C] FROM [Person] AS [Person]
 */

var sum = q.Sum(a => a.Age);
/*
 * SELECT CAST(SUM([Person].[Age]) AS INT) AS [C] FROM [Person] AS [Person]
 */

var max = q.Max(a => a.Age);
/*
 * SELECT MAX([Person].[Age]) AS [C] FROM [Person] AS [Person]
 */

var min = q.Min(a => a.Age);
/*
 * SELECT MIN([Person].[Age]) AS [C] FROM [Person] AS [Person]
 */

var avg = q.Average(a => a.Age);
/*
 * SELECT CAST(AVG([Person].[Age]) AS FLOAT) AS [C] FROM [Person] AS [Person]
 */