Why the EF core cannot compare DateTimeOffse?

Code like this
((a.DateOfRecive == null || b.DateOfRecive == null) || a.DateOfRecive.Value.Month == DateTimeOffset.Now.Month)

or
((a.DateOfRecive == null || b.DateOfRecive == null) || a.DateOfRecive.Value.Month == b.DateOfRecive.Value.Month)

EF core 3.1;
gives error
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 either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
 at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionvisitor.<VisitMethodCall>g__CheckTranslated|8_0(ShapedQueryExpression translated, <>c__DisplayClass8_0& )
 at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionvisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
 at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingexpressionvisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
 at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
 at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
 at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionvisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
 at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingexpressionvisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
 at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
 at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
 at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
 at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
 at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](database IDatabase, query Expression, IModel model, Boolean async)
 at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()
 at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
 at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
 at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
 at Microsoft.AspNetCore.Mvc.Infrastructure.AsyncEnumerableReader.ReadInternal[T](IAsyncEnumerable`1 value)
 at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsyncEnumerable(ActionContext context, ObjectResult result, IAsyncEnumerable`1 asyncEnumerable)
 at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker Task lastTask, State, next, Scope scope, Object state, Boolean isCompleted)
 at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
 at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
 at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
 at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker Task lastTask, State, next, Scope scope, Object state, Boolean isCompleted)
 at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
 at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
 at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
 at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Other settings all OK.
April 4th 20 at 13:28
1 answer
April 4th 20 at 13:30
EF does not execute the lambda in the sample, as, for example, when working with IEnumerable, the entire query is translated to SQL and executed, with easy types and functions are similar to the types and functions of SQL, if not unique I such an exception, overwrite the query or cast the types to simple, for example in your case set the DateTimeOffset.Now.Month integer variable before the query.

Find more questions by tags ASP.NETC#Entity Framework