英文:
LINQ query could not translate datetime exception
问题
我遇到了一个异常,我认为它发生在 LINQ 查询中尝试执行 y.Duration <= (int)(DateTime.Now - targetDateTime).TotalMinutes
时。请帮助我解决这个错误?
string dateString = "2023-06-04 08:34:46.2743289";
DateTime targetDateTime = DateTime.ParseExact(dateString, "yyyy-MM-dd HH:mm:ss.fffffff", null);
var myUser = _dbContext.Users
.Where(u => u.Telephome == telephone)
.Include(x => x.Activations)
.FirstOrDefaultAsync(u =>
u.Activations.Any(y =>
y.Code == code &&
y.Duration <= (int)(DateTime.Now - targetDateTime).TotalMinutes));
我最终遇到了以下异常:
LINQ 表达式 'DbSet
()
.Where(u0 => EF.Property<int?>(EntityShaperExpression:
Xchange.Core.Entities.User
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
, "Id") != null && object.Equals(
objA: (object)EF.Property<int?>(EntityShaperExpression:
Xchange.Core.Entities.User
ValueBufferExpression:
ProjectionBindingExpression: EmptyProjectionMember
IsNullable: False
, "Id"),
objB: (object)EF.Property<int?>(u0, "UserId")))
.Where(u0 => u0.Code == __code_1 && u0.Duration <= (int)(DateTime.Now - u0.TimeStamp).TotalMinutes)' 无法被
翻译。要么以可翻译的形式重写查询,要么明确切换到客户端评估,通过插入调用 'AsEnumerable'、'AsAsyncEnumerable'、'ToList' 或 'ToListAsync'。请参阅
https://go.microsoft.com/fwlink/?linkid=2101038 了解更多信息。
英文:
I am getting an exception, and I think when it occurs when it tries to execute y.Duration <= (int)(DateTime.Now - targetDateTime).TotalMinutes
in the LINQ query. Please help me sort this error?
string dateString = "2023-06-04 08:34:46.2743289";
DateTime targetDateTime = DateTime.ParseExact(dateString, "yyyy-MM-dd HH:mm:ss.fffffff", null);
var myUser = _dbContext.Users
.Where(u => u.Telephome== telephone)
.Include(x => x.Activations)
.FirstOrDefaultAsync(u => u.Activations.Any(y =>
y.Code == code &&
y.Duration <= (int)(DateTime.Now - targetDateTime).TotalMinutes));
I end up having the following exception thrown.
> The LINQ expression 'DbSet<Activation>()
> .Where(u0 => EF.Property<int?>(EntityShaperExpression:
> Xchange.Core.Entities.User
> ValueBufferExpression:
> ProjectionBindingExpression: EmptyProjectionMember
> IsNullable: False
> , "Id") != null && object.Equals(
> objA: (object)EF.Property<int?>(EntityShaperExpression:
> Xchange.Core.Entities.User
> ValueBufferExpression:
> ProjectionBindingExpression: EmptyProjectionMember
> IsNullable: False
> , "Id"),
> objB: (object)EF.Property<int?>(u0, "UserId")))
> .Where(u0 => u0.Code == __code_1 && u0.Duration <= (int)(DateTime.Now - u0.TimeStamp).TotalMinutes)' 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.
答案1
得分: 1
我见过类似的东西。问题在于Linq表达式必须被翻译(例如,序列化为SQL),但并非一切都有对应项。
然而,在你的情况下,解决方案应该很容易,因为关键的表达式可以提前(方便地)计算出来:
string dateString = "2023-06-04 08:34:46.2743289";
DateTime targetDateTime = DateTime.ParseExact(dateString, "yyyy-MM-dd HH:mm:ss.fffffff", null);
int duration = (int)((DateTime.Now - targetDateTime).TotalMinutes);
var myUser = _dbContext.Users
.Where(u => u.Telephome == telephone)
.Include(x => x.Activations)
.FirstOrDefaultAsync(u => u.Activations.Any(y =>
y.Code == code &&
y.Duration <= duration));
无论如何,在你的片段中,缺少一个闭括号(在最后)。
英文:
I've seen something like that. The problem is that the Linq expression has to be translated (e.g. serialized to SQL), but not everything has a counterpart.
However, in your case the solution should be easy, because the critical expression can be (conveniently) calculated in advance:
string dateString = "2023-06-04 08:34:46.2743289";
DateTime targetDateTime = DateTime.ParseExact(dateString, "yyyy-MM-dd HH:mm:ss.fffffff", null);
int duration = (int)((DateTime.Now - targetDateTime).TotalMinutes);
var myUser = _dbContext.Users
.Where(u => u.Telephome== telephone)
.Include(x => x.Activations)
.FirstOrDefaultAsync(u => u.Activations.Any(y =>
y.Code == code &&
y.Duration <= duration));
Anyway, in your snippet a closing parenthesis is missing (at the very end).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论