对EFCore Sqlite的Linq查询不起作用

flu*_*ter 3 c# sqlite linq-to-entities system.data.sqlite entity-framework-core

我正在使用EFCore SQLite,当使用Linq运行简单查询时,我注意到查询Take().Last()没有返回预期的内容,示例代码:

// There are more than 10000 rows in the table
DbSet<DataEntry> set = ctx.Set<DataEntry>();
// this should return the first 20 rows
var current = set.OrderBy(row => row.Id).Take(20);
// first entry is right
long tStart = current.First().Time;
// This should have returned the last entry in the first 20 entries, but instead, it returned the last entry of the entire data set
long tEnd = current.Last().Time;

// so essentially it looks like this:
set.Last().Time == set.Take(20).Last().Time
Run Code Online (Sandbox Code Playgroud)

它看起来Last将返回整个后备数据集中的最后一个条目,无论它基于哪个查询.我怎样才能解决这个问题?

Iva*_*oev 6

在EF Core 2.0.1,SqlServer中进行了测试和复制,因此这似乎是Last/ LastOrDefault一般查询翻译中的当前EF Core错误.

查询

var last = set.OrderBy(row => row.Id).Take(20).Last();
Run Code Online (Sandbox Code Playgroud)

翻译好像是

var last = set.OrderByDescending(row => row.Id).Take(20).First();
Run Code Online (Sandbox Code Playgroud)

这当然不是等价的,总是返回整个序列的最后一条记录(Take没有效果).

我建议将其报告给EF Core Issue Tracker.

同时,作为一种解决方法,您可以使用丑陋的显式等效项:

var last = set.OrderBy(row => row.Id).Take(20)
    .OrderByDescending(row => row.Id).First();
Run Code Online (Sandbox Code Playgroud)