第12章 聚合
EFCore 5 包含一些聚合数据的方法. 本章中, 我们会介绍 count, min, max, average, sum, 以及 group by. group by 字距通常与聚合函数一起使用. 我会首先简单介绍聚合函数, 然后介绍 group by.
设置单元测试
我们使用单元测试来介绍各种聚合函数. 在 DAL.Test 中创建一个新的 NUnit 类, 命名为 AggregationTest. 初始化 AppDbContext, 如同我们使用的其他单元测试一样. 你的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using EFCore5WebApp.Core.Entities;
using Microsoft.EntityFrameworkCore;
using NUnit.Framework;
namespace EFCore5WebApp.DAL.Tests {
[TestFixture]
public class AggregationTests {
private AppDbContext _context;
[SetUp]
public void SetUp() {
_context = new AppDbContext(
new DbContextOptionBuilder<AppDbContext>()
.UseSqlServer("... 链接字符串 ...")
.Options);
}
}
}