简体   繁体   English

对EFCore Sqlite的Linq查询不起作用

[英]Linq query on EFCore Sqlite does not work

I'm using EFCore SQLite, when run a simple query with Linq, I notice that the query Take().Last() does not return what is expected, sample code: 我正在使用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

It looks Last will return the last entry in the entire backing data set, no matter which query it is based on. 它看起来Last将返回整个支持数据集中的最后一个条目,无论它基于哪个查询。 How can I fix this? 我怎样才能解决这个问题?

Tested and reproduced in EF Core 2.0.1, SqlServer, hence this seems to be a current EF Core bug in Last / LastOrDefault general query translation. 在EF Core 2.0.1,SqlServer中进行了测试和复制,因此这似乎是Last / LastOrDefault常规查询转换中的当前EF Core错误。

The query 查询

var last = set.OrderBy(row => row.Id).Take(20).Last();

is translated as if it was 翻译好像是

var last = set.OrderByDescending(row => row.Id).Take(20).First();

which of course is not equivalent and always returns the last record of the whole sequence ( Take has no effect). 这当然不是等价的,总是返回整个序列的最后一条记录( Take没有效果)。

I would suggest reporting it to the EF Core Issue Tracker . 我建议将其报告给EF Core Issue Tracker

Meanwhile, as a workaround you could use the ugly explicit equivalent: 同时,作为一种解决方法,您可以使用丑陋的显式等效项:

var last = set.OrderBy(row => row.Id).Take(20)
    .OrderByDescending(row => row.Id).First();

Call ToList on current before grabbing first and last? 在抓住第一个和最后一个之前调用ToList on current

//...code removed for brevity.

var current = set.OrderBy(row => row.Id).Take(20).ToList();

//...code removed for brevity.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM