问题描述:事件查看器报如下错误(显示该C#系统自带字符串方法无法翻译为DB的表达式去查询数据库,实际上是取到数据的):
“WcfServiceHelper.CreateServerContext
(C:\Program Files (x86)\JetSun30\Bin\Core.dll:2021-04-15 16:12:30[Core:2021-04-15 16:12:30])
LINQ to Entities does not recognize the method 'Boolean IsNullOrWhiteSpace(System.String)' method, and this method cannot be translated into a store expression.(NotSupportedException)
---异常堆栈---
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(... ...
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()
at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at JetSun.Infrastructure.ServiceModel.ObjectContext`1.JetSun.Infrastructure.ServiceModel.IDataProvider.GetData(Expression filter, String orderKeys, Int32 maxCount, String[] includePaths)
at JetSun.Infrastructure.Advanced.WcfServiceHelper.GetDictionary(ServerContext serverContext, Type dtoType, Dbs dbs, Expression filter, Boolean getByClient, DtoDictionary setting)”
后来,将代码由 !string.IsNullOrWhiteSpace(a.FullName) 改为 a.FullName != null && a.FullName.Trim() != string.Empty 正常情况下这个改动确实避免了上面报错的发生;然而如果LAMBDA表达式过于冗长就会发生莫名其妙的问题(估计是超长部分被截断了,但是又没有报错),所以下面代码查询不到数据(dto为null就返回了)导致应该起来的作业调度没有如期启动。
只好把它改为原来的方式,一切正常(注意 a=>这段表达式不是很长的话,上面改动是正确无误的)