public static void GroupQueryTest() { MsSqlContext context = new MsSqlContext(DbHelper.ConnectionString); object ret = null; var q = context.Query <User>(); var r = q.GroupBy(a => a.Id).Having(a => a.Id > 1).Select(a => new { a.Id, Count = DbFunctions.Count(), Sum = DbFunctions.Sum(a.Id), Max = DbFunctions.Max(a.Id), Min = DbFunctions.Min(a.Id), Avg = DbFunctions.Average(a.Id) }).ToList(); q.GroupBy(a => a.Age).Having(a => a.Age > 1).Select(a => new { a.Age, Count = DbFunctions.Count(), Sum = DbFunctions.Sum(a.Age), Max = DbFunctions.Max(a.Age), Min = DbFunctions.Min(a.Age), Avg = DbFunctions.Average(a.Age) }).ToList(); var r1 = q.GroupBy(a => a.Age).Having(a => DbFunctions.Count() > 0).Select(a => new { a.Age, Count = DbFunctions.Count(), Sum = DbFunctions.Sum(a.Age), Max = DbFunctions.Max(a.Age), Min = DbFunctions.Min(a.Age), Avg = DbFunctions.Average(a.Age) }).ToList(); var g = q.GroupBy(a => a.Gender); //g = g.ThenBy(a => a.Name); //g = g.Having(a => a.Id > 0); //g = g.Having(a => a.Name.Length > 0); var gq = g.Select(a => new { Count = DbFunctions.Count() }); //gq = gq.Skip(1); //gq = gq.Take(100); //gq = gq.Where(a => a > -1); ret = gq.ToList(); var c = gq.Count(); ConsoleHelper.WriteLineAndReadKey(); }
public static void AggregateFunctionTest() { MsSqlContext context = new MsSqlContext(DbHelper.ConnectionString); var q = context.Query <User>(); q = q.Where(a => a.Id > 0); var xxx = q.Select(a => DbFunctions.Count()).First(); q.Select(a => new { Count = DbFunctions.Count(), LongCount = DbFunctions.LongCount(), Sum = DbFunctions.Sum(a.Age), Max = DbFunctions.Max(a.Age), Min = DbFunctions.Min(a.Age), Average = DbFunctions.Average(a.Age) }).First(); var count = q.Count(); var longCount = q.LongCount(); var sum = q.Sum(a => a.Age); var max = q.Max(a => a.Age); var min = q.Min(a => a.Age); var avg = q.Average(a => a.Age); Console.WriteLine(1); }