问题描述
我是一个新手,刚刚开始在一个项目中使用 EF Core 5,但遇到以下查询问题:
TimeSpan bookTimetoLive = TimeSpan.FromHours(10);
IList<Book>? expiredBooks = dbContext.Value.Books.AsQueryable()
.Where(x => DateTime.UtcNow - x.UtcTimeStamp > bookTimetoLive)
.ToList();
// Get list of expired Books to remove them
dbContext.Value.RemoveRange(expiredBooks);
await dbContext.Value.SaveChangesAsync(cancellationToken);
我的目标是删除所有已过期的图书(它们的时间戳已超过我想要跟踪它们的时间)。
有了这个,我得到了例外:
The LINQ expression 'DbSet<Books>()
.Where(d => DateTime.UtcNow - d.UtcTimeStamp > __bookTimetoLive_0)' 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.
经过一番挖掘,我意识到这是因为 EF 无法将我的 DateTime 比较解析为 sql 查询,因此我尝试使用 https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.sqlserverdbfunctionsextensions.datediffhour?view=efcore-5.0#Microsoft_EntityFrameworkCore_SqlServerDbFunctionsExtensions_DateDiffHour_Microsoft_EntityFrameworkCore_DbFunctions_System_DateTime_System_DateTime_
中的 DbFunctions.DateDiffHour() 方法现在的问题是,即使我在课堂上安装并导入了 Nuget EF Core 5,我也无法访问任何 DbFunctions 方法:
同样适用于 EF.Functions(无公共方法):
这可能是一个错误还是我没有正确使用这些类?这是我想要完成的正确方法吗? 干杯!
解决方法
如果时间分量是静态的(对于所有行都相同),那么一个简单的选择是将其应用于当前日期以形成一个截止值以进行比较:
代替:
TimeSpan bookTimeToLive = TimeSpan.FromHours(10);
IList<Book> expiredBooks = dbContext.Value.Books
.Where(x => DateTime.UtcNow - x.UtcTimeStamp > bookTimeToLive)
.ToList();
类似的东西。不需要 DbFunctions。
DateTime expiryCutoff = DateTime.UtcNow.AddHours(-10);
Ilist<Book> expiredBooks = dbContext.Books
.Where(x => x.UtTimeStamp < expiryCutoff)
.ToList();
如果是动态的,DateTime
之类的方法 AddHours
仍然会翻译:
Ilist<Book> expiredBooks = dbContext.Books
.Where(x => x.UtTimeStamp.AddHours(x.ExpiryCutoff) < DateTime.UtcNow)
.ToList();
其中 ExpiryCutoff 是记录中的数据驱动值。 (或相关表达)
,或者,我们可以只使用 DateTime
对象并重写您的原始查询吗?
IList<Book>? expiredBooks = dbContext.Value.Books
.Where(x => x.UtcTimeStamp.addHours(10) > DateTime.UtcNow)
.ToList();