LINQ查询无法翻译日期时间异常。

huangapple go评论66阅读模式
英文:

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 &lt;= (int)(DateTime.Now - targetDateTime).TotalMinutes in the LINQ query. Please help me sort this error?

string dateString = &quot;2023-06-04 08:34:46.2743289&quot;;
DateTime targetDateTime = DateTime.ParseExact(dateString, &quot;yyyy-MM-dd HH:mm:ss.fffffff&quot;, null);

var myUser =  _dbContext.Users
           .Where(u =&gt; u.Telephome== telephone)
           .Include(x =&gt; x.Activations)
           .FirstOrDefaultAsync(u =&gt; u.Activations.Any(y =&gt;
                y.Code == code &amp;&amp;
                y.Duration &lt;= (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 = &quot;2023-06-04 08:34:46.2743289&quot;;
DateTime targetDateTime = DateTime.ParseExact(dateString, &quot;yyyy-MM-dd HH:mm:ss.fffffff&quot;, null);

int duration = (int)((DateTime.Now - targetDateTime).TotalMinutes);

var myUser =  _dbContext.Users
           .Where(u =&gt; u.Telephome== telephone)
           .Include(x =&gt; x.Activations)
           .FirstOrDefaultAsync(u =&gt; u.Activations.Any(y =&gt;
                y.Code == code &amp;&amp;
                y.Duration &lt;= duration));

Anyway, in your snippet a closing parenthesis is missing (at the very end).

huangapple
  • 本文由 发表于 2023年6月5日 11:29:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定