我们正在使用 .NET Core 3.1、Microsoft.EntityFrameworkCore 3.1.9 和 Npgsql 4.1.9。我们有以下简单的
TestExecution
脚手架类:
[Table("test_execution", Schema = "test")]
public partial class TestExecution
{
[Key]
[Column("id")]
public int Id { get; set; }
[Column("test_result")]
public string TestResult { get; set; }
[Column("name")]
public string Name { get; set; }
}
PostgreSQL 数据库中的数据如下所示:
id | 测试结果 | 姓名 |
---|---|---|
1 | 好 | 测试 1 |
2 | 好 | 测试 2 |
3 | 错误 | 测试 3 |
4 | 错误 | 测试 4 |
5 | 好 | 测试 5 |
6 | 好 | 测试 6 |
7 | 警告 | 测试 7 |
对于相同连续
id
值的序列,我们希望返回具有最小test_result
的行。我们的预期输出是:
id | 测试结果 | 姓名 |
---|---|---|
7 | 警告 | 测试 7 |
5 | 好 | 测试 5 |
3 | 错误 | 测试 3 |
1 | 好 | 测试 1 |
这可以通过以下 SQL 查询来实现:
SELECT
id,
test_result,
name
FROM (
SELECT
id,
test_result,
name,
LEAD(test_result) OVER (ORDER BY id DESC) AS next_result
FROM fit.test_execution
) s
WHERE
test_result is distinct from next_result;
我们如何在不编写任何原始 SQL 的情况下通过 EF Core 3.1 和 Linq 生成此 SQL 查询?
EF Core 当前不支持窗口函数 - 由this issue跟踪。