SQLite 数据库表由四列组成:
Id = table.Column<Guid>(type: "TEXT", nullable: false),
Date = table.Column<DateTime>(type: "TEXT", nullable: false),
Axen = table.Column<bool>(type: "INTEGER", nullable: true),
Doran = table.Column<bool>(type: "INTEGER", nullable: true)
目标是在
Date
和 Axen
为真时获得最年轻的行(通过 Doran
)。
使用以下 LINQ 表达式:
return db.DayRecord.OrderBy(x => x.Date).TakeWhile(x => x.Axen == true && x.Doran == true).ToList();
期望从最年轻的行中取出行(使用
OrderBy
),而Axen
和Doran
为真。
实际结果是一条错误消息:
System.InvalidOperationException
HResult=0x80131509
Message=The LINQ expression 'DbSet<DayRecord>()
.OrderBy(x => x.Date)
.TakeWhile(x => x.Axen == (bool?)True && x.Doran == (bool?)True)' could not be translated.
Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
虽然语句末尾有
ToList()
调用,但预期查询语法无效。
获取数据的有效语法是什么?
TakeWhile
支持和不支持的linq方法。
您可以使用Where方法
return db.DayRecord.Where(x => x.Axen == true && x.Doran == true).OrderBy(x => x.Date).ToList();