C# “linq”有一个错误;System.NotSupportedException“;

C# “linq”有一个错误;System.NotSupportedException“;,c#,entity-framework,linq,rest,C#,Entity Framework,Linq,Rest,您是否可以帮助解决此错误的问题,或者说明如何以不同的方式执行此错误。谢谢大家! 我的任务是输出结果(集合),其中包含每个实体的注释。 如果用户已将其选中 protected Expression < Func < TEntity, TBindingModel > > MapToBindingModel; protected ApplicationUserManager UserManager => Request.GetOwinContext().GetUser

您是否可以帮助解决此错误的问题,或者说明如何以不同的方式执行此错误。谢谢大家!

我的任务是输出结果(集合),其中包含每个实体的注释。 如果用户已将其选中

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)       
    };       
protectedexpression>映射到绑定模型;
受保护的ApplicationUserManager UserManager=>Request.GetOwinContext().GetUserManager();
内部应用程序用户\u应用程序用户;
[HttpGet]
[路线]
[启用查询]
公共虚拟异步任务GetAll()
{
_applicationUser=await UserManager.FindByIdAsync(User.Identity.GetUserId());
if(_applicationUser==null)
返回请求(“未找到用户”);
返回Ok(Service.GetAll().Select(MapToBindingModel));
}
MapToBindingModel=标题=>新标题绑定模型
{
Id=title.Id,
//数据。。
Favorite=\u applicationUser!=null&&u applicationUser.Favorite.Titles.Any(i=>i.Id==title.Id)
};       
以下是错误消息:

{
    "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()"
}
{
“消息”:“发生错误。”,
“ExceptionMessage”:“无法创建类型为”Domain.Authorization.ApplicationUser \“的常量。在此上下文中,仅支持基元类型和枚举类型。”,
“异常类型”:“System.NotSupportedException”,
“StackTrace”:“of System.Data.Entity.Core.Objects.Elink.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter父级,ConstantExpression linq)\r\n of System.Data.Entity.Core.Objects.Elink.ExpressionConverter.TranslateExpression(Expression linq)\r\nöSystem.Data.Entity.Core.Objects.ELinq.ExpressionConverter.EqualsTranslator.CreateIsNullExpression(ExpressionConverter父级,表达式输入)\r\nöSystem.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式linq)\r\nöSystem.Data.Entity.Core.Objects.ELinq.ExpressionConverter.NotTranslator.TypedTranslate(ExpressionConverter父级,UnaryExpression linq)\r\nöSystem.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)\r\nöSystem.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式linq)\r\nöSystem.Data.Entity.Core.Objects.ELinq.ExpressionConverter.BinaryTranslator.TypedTranslate(ExpressionConverter父级,BinaryExpression linq)\r\nöSystem.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式linq)\r\nöSystem.Data.Entity.Core.Objects.Eliq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter父级,MemberInitExpression linq)\r\nöSystem.Data.Entity.Core.Objects.Eliq.ExpressionConverter.TranslateExpression(Expression linq)\r\nöSystem.Data.Entity.Core.Objects.Eliq.ExpressionConverter.TranslateLambda(LambdaExpression lambda,DbExpression-input)\r\nöSystem.Data.Entity.Core.Objects.Elink.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter父级,MethodCallExpression调用,DbExpression&source,DbExpressionBinding&sourceBinding,DbExpression&lambda)\r\nöSystem.Data.Entity.Core.Objects.Eliq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter父级,MethodCallExpression调用)\r\nöSystem.Data.Entity.Core.Objects.Eliq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter父级,MethodCallExpression linq)\r\nöSystem.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(表达式linq)\r\nöSystem.Data.Entity.Core.OBJECTOR.ELinq.EXPRESSIONCONVERT()\r\nöSystem.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(可为null`1 forMergeOption)\r\nöSystem.Data.Entity.Core.Objects.ObjectQuery`1.c_uuuDisplayClass7.b_uuu6()\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__显示类7.b___5()\r\nöSystem.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1操作)r\nöSystem.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(可为null`1 forMergeOption)\r\nöSystem.Data.Entity.Core.ObjectQuery`1.b_0()\r\nöSystem.Data.Entity.Internal.LazyEnumerator`1.MoveNext()\r\nöNewtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter编写器、IEnumerable值、JsonArrayContract合约、JsonProperty成员、JsonContainerContract collectionContract、JsonProperty容器属性)\r\nöNewtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter JsonWriter,对象值,类型objectType)\r\nöNewtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter JsonWriter,对象值,类型objectType)\r\nöSystem.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型类型、对象值、流writeStream、编码有效编码)\r\nöSystem.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(类型、对象值、流writeStream、编码有效编码)\r\nöSystem.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型类型、对象值、流写入流、HttpContent内容、TransportContext TransportContext、CancellationToken CancellationToken)\r\n---\r\nöSystem.Web.Http.Owin.HttpMessageHandlerAdapter.d_u27.MoveNext()
}
之前需要一个
ToList()
。选择(MapToBindingModel)

由于EF无法将您的结果投影到
TitleBindingM
public virtual async Task<IHttpActionResult> GetAll()
{
    ...

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