LINQ to Entities does not recognize the method 'System.String StringConvert(System.Nullable`1[System.Double])
Solution 1
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;
Solution 2
You don't have to convert it to string
, string concatenation will take care of that.
AnncTitle = n.AnncTitle + " <a href='bulletinAdd.aspx?ID=" + n.RecID + "'><Edit></a>",
You don't have to call ToString
as well.
Related videos on Youtube
Jennifer L
Updated on July 09, 2022Comments
-
Jennifer L almost 2 years
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
-
Tim S. over 10 yearsIs it just my imagination, or are you putting view concerns (HTML code generation) into your data access? That's a bit smelly.
-
Jennifer L over 10 years@tim-s I'm not sure how else to do it. What is shown in my datalist is dependent on what data is returned. I'm certainly not a master programmer though so I'd welcome any suggestions.
-
Vishal Sharma over 10 years@JenniferL not all linq extenstions methods are supported pls see ..msdn.microsoft.com/en-us/library/bb738550.aspx
-
Elsimer over 10 yearsare you using SQL Server as a provider?entityframework.codeplex.com/workitem/1774
-
-
pescolino over 10 yearsBut the exception says EF doesn't recognize
StringConvert(double? value)
. Why? This method exists solely for exact that usage. What am I missing? -
Habib over 10 years@pescolino, I am not sure but I would try it calling like
SqlFunctions.StringConvert((double?)n.RecID)
-
Jennifer L over 10 yearsThanks @pescolino and @Habib! I tried removing StringConvert and got Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types. When I add the ? after double I get the original error
-
hazimdikenli about 10 yearssame thing, I got this error after upgrading to EF6, and it was working after changing to the correct assembly.
-
B Bulfin over 9 yearsThis must have happened when EF was extracted from the standard .net framework and moved to a separate assembly. It's unfortunate.
-
Ricardo Souza almost 9 yearsAn what can I do if I'm using MySql?
-
Paul almost 9 yearsAlso, ensure that EntityFramework.SqlServer is referenced.
-
Matt over 8 yearsThis is definitely the correct answer in this case. Reverting to LINQ to Objects should be the very last resort...