首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么此查询返回System.InvalidOperationException

为什么此查询返回System.InvalidOperationException
EN

Stack Overflow用户
提问于 2022-11-16 06:43:17
回答 1查看 72关注 0票数 3

我想根据一定的条件来统计学生

代码语言:javascript
运行
复制
return _context.Students.Where(x => 
          (x.Batch1 > 0 || x.Batch2 > 0) && 
           x.Received == 0 || 
           (DateTime.Now.Subtract(x.DateOfReg).Days >= 30 && 
          x.Received < 2000 && x.Payment >= 2000) || 
          (DateTime.Now.Subtract(x.DateOfReg).Days >= 120 && x.Due != 0)).Count();

但这会导致这些错误-

代码语言:javascript
运行
复制
System.InvalidOperationException: The LINQ expression 'DbSet<Student>()
    .Where(s => s.Batch1 > 0 || s.Batch2 > 0 && s.Received == 0 || DateTime.Now.Subtract(s.DateOfReg).Days >= 30 && s.Received < 2000 && s.Payment >= 2000 || DateTime.Now.Subtract(s.DateOfReg).Days >= 120 && s.Due != 0)' could not be translated. Additional information: Translation of method 'System.DateTime.Subtract' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information.
Translation of method 'System.DateTime.Subtract' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information.
Translation of method 'System.DateTime.Subtract' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information.
Translation of method 'System.DateTime.Subtract' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. 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.
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|15_0(ShapedQueryExpression translated, <>c__DisplayClass15_0& )
   at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.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 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.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
   at StudentManagement.Controllers.StudentController.CountByRoutine(Int32 batch, Int32 payment) in C:\Users\Nazmul\Box\Project\.NET CORE\StudentManagement\Controllers\StudentController.cs:line 256
   at lambda_method370(Closure , Object , Object[] )

我不知道为什么会这样。在我看来我的情况是正确的。我遗漏了什么?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-16 06:59:13

它失败了,因为LINQ无法将C#方法DateTime.Subtract转换为SQL中的等效函数。

您必须使用类似于DiffDays()或类似于System.Data.Entity.DbFunctions的方法,这些方法被完全映射到sql方法。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74456190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档