c# – LINQ to Entities不识别方法’System.String StringConvert(System.Nullable`1 [System.Double])

前端之家收集整理的这篇文章主要介绍了c# – LINQ to Entities不识别方法’System.String StringConvert(System.Nullable`1 [System.Double])前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我不知道为什么我得到这个错误.我已经使用这个功能与以前版本的实体框架,但我已经设置了一个新的项目使用EF6,它不合作.
using System.Data;
using System.Data.Objects.sqlClient;

e.Result = from n in MyDB.tblBulletins
     where n.AnncStart <= DateTime.Now && n.AnncEnd > DateTime.Now && n.Approved == true
     orderby n.AnncStart descending,n.AnncDate descending
     select new
     {
        n.RecID,AnncTitle = n.AnncTitle + " <a href='bulletinAdd.aspx?ID=" + sqlFunctions.StringConvert((double)n.RecID).Trim() + "'><Edit></a>",AnncText = (n.AnncImg == null ? n.AnncText : "<a href='images/bulletin/" + n.AnncImg + "'><img src='images/bulletin/" + n.AnncImg + "' class='stickyphoto' alt='Click for larger image'/></a>" + n.AnncText),Email = (n.Email == null ? "" : "<br/><a href='mailto:" + n.Email + "'>" + n.Email + "</a>"),n.AnncType,n.AnncDate,n.AnncEnd,n.v_EmpBasicInfo.Name
      };

当我运行这个我得到
LINQ to Entities不会识别方法’System.String StringConvert(System.Nullable`1 [System.Double])’方法,并且此方法不能转换为存储表达式.

n.RecID是sql数据库中的表上的一个int主键(sql Server Standard Edition)

所有我似乎通过搜索找到的人是推荐StringConvert而不是ToString

添加 – 堆栈跟踪:

[NotSupportedException: LINQ to Entities does not recognize the method 'System.String StringConvert(System.Nullable`1[System.Double])' method,and this method cannot be translated into a store expression.]
System.Data.Entity.Core.Objects.ELinq.DefaultTranslator.Translate(ExpressionConverter parent,MethodCallExpression call) +194
System.Data.Entity.Core.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent,MethodCallExpression linq) +976
System.Data.Entity.Core.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent,Expression linq) +88
System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +148
System.Data.Entity.Core.Objects.ELinq.BinaryTranslator.TypedTranslate(ExpressionConverter parent,BinaryExpression linq) +122
System.Data.Entity.Core.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent,BinaryExpression linq) +87
System.Data.Entity.Core.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent,Expression linq) +88
System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +148
System.Data.Entity.Core.Objects.ELinq.NewTranslator.TypedTranslate(ExpressionConverter parent,NewExpression linq) +520
System.Data.Entity.Core.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent,Expression linq) +88
System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +148
System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda,DbExpression input) +168
System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda,DbExpression input,DbExpressionBinding& binding) +160
System.Data.Entity.Core.Objects.ELinq.OneLambdaTranslator.Translate(ExpressionConverter parent,MethodCallExpression call,DbExpression& source,DbExpressionBinding& sourceBinding,DbExpression& lambda) +168
System.Data.Entity.Core.Objects.ELinq.SelectTranslator.Translate(ExpressionConverter parent,MethodCallExpression call) +70
System.Data.Entity.Core.Objects.ELinq.SequenceMethodTranslator.Translate(ExpressionConverter parent,SequenceMethod sequenceMethod) +47
System.Data.Entity.Core.Objects.ELinq.MethodCallTranslator.TypedTranslate(ExpressionConverter parent,MethodCallExpression linq) +141
System.Data.Entity.Core.Objects.ELinq.TypedTranslator`1.Translate(ExpressionConverter parent,Expression linq) +88
System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) +148
System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert() +50
System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) +563
System.Data.Entity.Core.Objects.<>c__DisplayClassb.<GetResults>b__a() +83
System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func,IDbExecutionStrategy executionStrategy,Boolean startLocalTransaction,Boolean releaseConnectionOnSuccess) +499
System.Data.Entity.Core.Objects.<>c__DisplayClassb.<GetResults>b__9() +271
System.Data.Entity.sqlServer.DefaultsqlExecutionStrategy.Execute(Func`1 operation) +251
System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) +600
   System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() +89
System.Lazy`1.CreateValue() +416
System.Lazy`1.LazyInitValue() +152
System.Lazy`1.get_Value() +75
System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() +40
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +381
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
System.RuntimeMethodHandle.InvokeMethod(Object target,Object[] arguments,Signature sig,Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,Object[] parameters,Object[] arguments) +92
System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,CultureInfo culture) +108
System.Reflection.MethodBase.Invoke(Object obj,Object[] parameters) +19
System.Web.UI.WebControls.QueryableDataSourceHelper.ToList(IQueryable query,Type dataObjectType) +225
System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +549
System.Web.UI.WebControls.Repeater.GetData() +55
System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +89
System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +61
System.Web.UI.WebControls.Repeater.DataBind() +105
System.Web.UI.WebControls.Repeater.EnsureDataBound() +49
System.Web.UI.WebControls.Repeater.OnPreRender(EventArgs e) +15
System.Web.UI.Control.PreRenderRecursiveInternal() +83
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Control.PreRenderRecursiveInternal() +168
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint) +974

解决方法

我有同样的问题,意识到我正在使用错误sqlFunction类型.请确保您引用了正确的命名空间:
using System.Data.Entity.sqlServer;

并不是:

using System.Data.Objects.sqlClient;
原文链接:/csharp/95390.html

猜你在找的C#相关文章