LINQ to Entities 无法识别方法 'System.String ToString()'.尝试解析日期时间时
本文关键字:时间 日期 System Entities to 识别 String LINQ 方法 ToString | 更新日期: 2023-09-27 18:26:21
我正在尝试从实体查询中获取自定义数据集,但幸运的是我收到以下错误:
LINQ to Entities 无法识别方法 System.String ToString(( 方法,并且此方法无法转换为存储表达式。 下面是返回数据的自定义类:
public class featuredlist
{
public string kodg { get; set; }
public string excursion { get; set; }
public string excursiondate { get; set; }
}
public IQueryable<featuredlist> GetFeatured(string fdate)
{
try
{
var fd = DateTime.Parse(fdate);
var uname = HttpContext.Current.User.Identity.Name;
var uid = (from x in db.xEmployee where x.Сотрудник.Contains(uname)
select x.Kod).FirstOrDefault();
var fl = (from a in db.xGrafik
join b in db.xExcursion on a.Excursion equals b.Kod
join c in db.xDate on a.КодД equals c.Kod
join d in db.xOrder on a.Kod equals d.КодГ
where c.Дата == fd & d.Продал == uid
orderby c.Дата descending
select new featuredlist
{
kodg = a.Kod.ToString(),
excursion = b.Название,
//Probably error happend here.
excursiondate =
(c.Дата) != null ?
c.Дата.Value.ToShortDateString :
"01.01"
})
.AsQueryable();
return fl.Distinct();
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
var path = @"d:'error.txt";
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Entity of type '"{0}'" in state '"{1}'" has
the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
}
using (StreamWriter sw1 = File.CreateText(@"d:'error1.txt"))
foreach (var ve in eve.ValidationErrors)
{
sw1.WriteLine("- Property: '"{0}'", Error: '"{1}'"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
}
异常消息:"LINQ to Entities 无法识别方法'System.String ToString(('方法,并且此方法无法转换为存储表达式。 堆栈跟踪: " в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent, MethodCallExpression call( в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq( в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator
1.Translate(ExpressionConverter parent, Expression linq) в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq) в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator
1.Translate(ExpressionConverter parent, Expression linq( вSystem.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq( в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input( в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding( вSystem.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.translate(ExpressionConverter parent, MethodCallExpression callcall call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda( в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call( вSystem.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod( в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq( в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator1.Translate(ExpressionConverter parent, Expression linq) в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateSet(Expression linq) в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.UnarySequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator
1.Translate(ExpressionConverterparent, Expression linq( в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq( в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert(( в System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable1 forMergeOption) в System.Data.Entity.Core.Objects.ObjectQuery
1.<>c__DisplayClassb.b__a(( в System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) в System.Data.Entity.Core.Objects.ObjectQuery
1.<>c__DisplayClassb.b__9(( вSystem.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation) в System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(Nullable1 forMergeOption) в System.Data.Entity.Core.Objects.ObjectQuery
1..GetEnumerator>b__0(( в System.Lazy1.CreateValue() в System.Lazy
1.LazyInitValue(( в System.Lazy1.get_Value() в System.Data.Entity.Internal.LazyEnumerator
1.MoveNext(( в Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty( в Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty( в Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType( в Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType( в Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value( в System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding Effective Encoding( в System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding Effective Encoding( в System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content( вSystem.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancelToken cancelToken( --- Конец трассировка стека из предыдущего расположения, где возникло исключение --- в System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task( в System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task( вSystem.Runtime.CompilerServices.TaskAwaiter.GetResult(( в System.Web.Http.WebHost.HttpControllerHandler.d__1b.移动下一个((">
fl
查询中的所有内容都必须可转换为有效的 SQL。 ToString
不是,您必须改用SqlFunctions.StringConvert
:
kodg = SqlFunctions.StringConvert(a.Kod),
或者,您可以先检索结果(AsEnumerable
(,然后在非进程查询中检索项目(select
(。
问题在于必须返回自定义数据的自定义类。当我将其更改为如下(日期时间?而不是字符串(时,它工作正常:
public class featuredlist
{
public int kodg { get; set; }
public string excursion { get; set; }
public DateTime? excursiondate { get; set; }
}