简体   繁体   English

linq“System.NotSupportedException”出错

[英]There is an error with linq “System.NotSupportedException”

Could you help to solve the problem with this error or say how it can be done differently. 你能帮忙解决这个错误的问题,或者说如何以不同的方式完成。 Thank you! 谢谢!

My task is to output the result (collection) with a note for each entity in it. 我的任务是输出结果(集合),其中包含每个实体的注释。 If the user has it in the elected. 如果用户在当选中拥有它。

protected Expression < Func < TEntity, TBindingModel > > MapToBindingModel;  
protected ApplicationUserManager UserManager => Request.GetOwinContext().GetUserManager<ApplicationUserManager>();


internal ApplicationUser _applicationUser;

[HttpGet]
[Route]
[EnableQuery]
public virtual async Task<IHttpActionResult> GetAll()
{
    _applicationUser = await UserManager.FindByIdAsync(User.Identity.GetUserId());
    if (_applicationUser == null)
        return BadRequest("User is not found");

    return Ok(Service.GetAll().Select(MapToBindingModel));
}

MapToBindingModel = title => new TitleBindingModel
    {
        Id = title.Id,
       //data..
        Favorite = _applicationUser != null && _applicationUser.Favorite.Titles.Any(i=>i.Id == title.Id)       
    };       

Here is the error message: 这是错误消息:

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "Could not create constant with type \"Domain.Authorization.ApplicationUser\". In this context only primitive types and enumeration types are supported.",
    "ExceptionType": "System.NotSupportedException",
    "StackTrace": "   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.EqualsTranslator.CreateIsNullExpression(ExpressionConverter parent, Expression input)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.NotTranslator.TypedTranslate(ExpressionConverter parent, UnaryExpression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.BinaryTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)\r\n   в System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()\r\n   в System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)\r\n   в System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()\r\n   в System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)\r\n   в System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()\r\n   в System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)\r\n   в System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)\r\n   в System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()\r\n   в System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()\r\n   в Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   в Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n   в Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n   в System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n   в System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n   в System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- Конец трассировка стека из предыдущего расположения, где возникло исключение ---\r\n   в System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   в System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   в System.Web.Http.Owin.HttpMessageHandlerAdapter.<BufferResponseContentAsync>d__27.MoveNext()"
}

You need a ToList() before .Select(MapToBindingModel) 你需要一个ToList() .Select(MapToBindingModel)

Since EF can't project your results to TitleBindingModel you should materialize the result of GetAll() before attempting to map them. 由于EF无法将结果TitleBindingModelTitleBindingModel ,因此您应该在尝试映射之前实现GetAll()的结果。

public virtual async Task<IHttpActionResult> GetAll()
{
    ...

    return Ok(Service.GetAll().ToList().Select(MapToBindingModel));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ错误:GroupBy选择上出现System.NotSupportedException - LINQ error: System.NotSupportedException on GroupBy Selection LINQ GroupBy子句System.NotSupportedException错误 - LINQ GroupBy Clause System.NotSupportedException error 按日期时间过滤 LINQ 查询导致 System.NotSupportedException 错误 - Filtering a LINQ query by DateTime causing System.NotSupportedException Error System.NotSupportedException使用DateTime吗? 在Linq to实体 - System.NotSupportedException using DateTime? in Linq to Entities LINQ嵌套查询问题-System.NotSupportedException - LINQ nested query issue - System.NotSupportedException 发生System.NotSupportedException - System.NotSupportedException occuring WebRequest System.NotSupportedException - WebRequest System.NotSupportedException Linq to EF系统不支持的异常System.NotSupportedException - Linq to EF system not supported exception System.NotSupportedException LINQ to Entities:“选择新”表达式树将引发System.NotSupportedException - LINQ to Entities : “select new” expression tree throws System.NotSupportedException System.NotSupportedException:&#39;LINQ to Entities中不支持指定的类型成员&#39;StartDateTime&#39; - System.NotSupportedException: 'The specified type member 'StartDateTime' is not supported in LINQ to Entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM