Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

c# - LINQ to Entities does not recognize the method 'System.String StringConvert(System.Nullable`1[System.Double])

I can't figure out why I'm getting this error. I have used this function successfully with previous versions of Entity Framework but I've set up a new project using EF6 and it's not cooperating.

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
      };

When I run this I get 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.

n.RecID is an int primary key on the table in a SQL database (SQL Server Standard Edition)

All I can seem to find through searches is people recommending StringConvert instead of ToString

ADDITION - Stack Trace:

[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, 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) +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, MethodCallExpression call, 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, Object[] parameters, 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same problem and realized that I was using the wrong type of SqlFunctions. Make sure that you referenced the correct namespace:

using System.Data.Entity.SqlServer;

and not:

using System.Data.Objects.SqlClient;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...