Core 3.x LINQ -> 日期时间和字符串比较错误



我编写的代码如下。

purchaseList = (from tp in context.TbPurchasebill
where
tp.CompanyNo == companyNo &&
tp.BillDate.ToString("yyyy-MM").Contains(billDate) &&
tp.DeleteTf == false
orderby
tp.BusinessName
select new PurchaseList
{
BillNo = tp.BillNo,
BillDate = tp.BillDate,
BusinessName = tp.BusinessName,
GoodsName = tp.GoodsName
}).ToList();

tp。账单日期。ToString("yyyy-MM")。在Contents(billDate)中,billDate是DateTime格式,而billDate是值"的字符串;2021-05";。

执行代码将导致以下错误:

系统。InvalidOperationException:LINQ表达式'数据库集.其中(t=>t.CompanyNo==__CompanyNo_0&&amp;(可为空)t。BillDate!=空&amp;t.BillDate.ToString("yyyy-MM")。包含(__BillDate_1)&amp;t.删除Tf==(可为null)False)"无法翻译。要么重写以可翻译的形式查询,或切换到客户端评估通过插入对AsEnumerable()的调用,AsAsyncEnumerable()、ToList()或ToListAsync()。看见https://go.microsoft.com/fwlink/?linkid=2101038了解更多信息。在微软EntityFrameworkCore。查询QueryableMethodTranslationExpressionVisitor.g_CheckTranslated|8_0(ShapedQueryExpression翻译,<gt;c__DisplayClass8_0&)在微软EntityFrameworkCore。查询QueryableMethodTranslationExpressionVisitor。VisitMethodCall(MethodCallExpressionmethodCallExpression)微软EntityFrameworkCore。查询RelationalQueryableMethodTranslationExpressionVisitor。VisitMethodCall(MethodCallExpressionmethodCallExpression)系统林克。表达式。MethodCallExpression。接受(ExpressionVisitor访问者)系统林克。表达式。ExpressionVisitor。访问(表达式节点)微软EntityFrameworkCore。查询QueryableMethodTranslationExpressionVisitor。VisitMethodCall(MethodCallExpressionmethodCallExpression)微软EntityFrameworkCore。查询RelationalQueryableMethodTranslationExpressionVisitor。VisitMethodCall(MethodCallExpressionmethodCallExpression)系统林克。表达式。MethodCallExpression。接受(ExpressionVisitor访问者)系统林克。表达式。ExpressionVisitor。访问(表达式节点)微软EntityFrameworkCore。查询QueryableMethodTranslationExpressionVisitor。VisitMethodCall(MethodCallExpressionmethodCallExpression)微软EntityFrameworkCore。查询RelationalQueryableMethodTranslationExpressionVisitor。VisitMethodCall(MethodCallExpressionmethodCallExpression)系统林克。表达式。MethodCallExpression。接受(ExpressionVisitor访问者)系统林克。表达式。ExpressionVisitor。访问(表达式节点)微软EntityFrameworkCore。查询QueryCompilationContext。CreateQueryExecutor[TResult](表达式查询)微软EntityFrameworkCore。存储数据库CompileQuery[TResult](表达式查询,布尔异步)微软EntityFrameworkCore。查询内部的查询编译器。CompileQueryCore[TResult](IDatabase数据库,表达式查询,IModel模型,布尔异步)微软EntityFrameworkCore。查询内部的查询编译器<gt;c__DisplayClass9_01.<Execute>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1编译器)微软EntityFrameworkCore。查询内部的CompiledQueryCache。GetOrAddQuery[TResult](对象cacheKey,函数1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.GetEnumerator()在系统中。集合。通用的在系统中列出1..ctor(IEnumerable1集合)
。林克。可枚举。ToList[TSource](IEnumerable`1源)位于AccountManagerData。存储库。GetPurchaseList(Int32 companyNo,字符串billDate)C: \Users\Administrator\source\repos\AccountManager\AccountManager Data\Repository.cs:line3573会计经理。控制器。账单采购管理员。GetPurchaseList(PurchaseForListFind购买)C: \Users\Administrator\source\repos\AccountManager\AccountManager\Controllers\Bill\PurchaseController.cs:line30处的lambda_method(闭包,Object,Object[])微软扩展。内部的ObjectMethodExecutor。执行(对象target,Object[]参数)微软AspNetCore。Mvc。基础设施ActionMethodExecutor。SyncActionResultExecutor。执行(IActionResultTypeMapper映射程序,ObjectMethodExecutor executor,Object控制器,Object[]arguments)微软AspNetCore。Mvc。基础设施ControllerActionInvoker。InvokeActionMethodAsync()在微软AspNetCore。Mvc。基础设施ControllerActionInvoker。下一个(状态&下一步,Scope&作用域,对象&state,布尔&isCompleted)微软AspNetCore。Mvc。基础设施ControllerActionInvoker。InvokeNextActionFilterAsync()---从上一个引发异常的位置开始的堆栈结尾跟踪--在微软AspNetCore。Mvc。基础设施ControllerActionInvoker。Rethrow(ActionExecutedContextSealed上下文)微软AspNetCore。Mvc。基础设施ControllerActionInvoker。下一个(状态&下一步,Scope&作用域,对象&state,布尔&isCompleted)微软AspNetCore。Mvc。基础设施ControllerActionInvoker。InvokeInnerFilterAsync()---从上一个引发异常的位置开始的堆栈结尾跟踪--在Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g_Await|19_0(资源调用程序invoker,Task lastTask,State next,Scope Scope,Object State,BooleanisCompleted)Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g_Await|17_0(资源调用程序invoker、Task Task、IDisposable作用域)Microsoft.AspNetCore.Routing.EndpointMiddleware.g_AwaitRequestTask|6_0(端点端点,任务请求任务,ILogger记录器)Microsoft.AspNetCore.Authorization.AuthenticationMiddleware.IInvoke(HttpContext上下文)Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.IInvoke(HttpContext上下文)Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.IInvoke(HttpContexthttpContext)Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.IInvoke(HttpContexthttpContext,ISwakerProvider swaggerProvider)Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.IInvoke(HttpContext上下文)


要修复错误,可以在末尾运行"AsEnumerable()"并调用再一次"在哪里"。

除此之外还有其他方法吗?

我认为你必须更换

tp.BillDate.ToString("yyyy-MM").Contains(billDate) &&

带有

tp.BillDate.ToString("yyyy-MM") == billDate &&

我认为没有太多问题需要解决。

我只是决定做以下事情。

int year=billDate.split('-')[0];int month=billDate.split('-')[1];

tp.BillDate.Year==年份&amp;tp。BillDate.Month==月;

最新更新