我创建了一个restful服务,它返回一个List实体类型:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class RealAgent : IRealAgent
{
public IList<RealUser> GetAll()
{
return UserManager.Agents();
}
}
//用户管理器类实现
public static List<RealUser> Agents()
{
var q = from x in DBContextHelper.DBContext.RealUsers
orderby x.CreatedOn descending
select new RealUser()
{
FirstName = x.FirstName,
LastName =x.LastName,
AboutProfession = x.AboutProfession
};
return q.ToList();
}
//我正在获取所选列。但它没有用。我是否需要创建自己的类OR是否可以使用Entity Framework实现?
错误: 服务器遇到处理请求的错误。 LINQ to Entities查询中无法构造异常消息'实体或复杂类型'GharbarIndia.Data.RealUser'。请参阅服务器日志以获取更多详异常堆栈跟踪是:
System.Data.Objects.ELinq.ExpressionConverter上System.Data.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent,MemberInitExpression linq)中的System.Data.Objects.ELinq.ExpressionConverter.CheckInitializerType(Type type)的 .TypedTranslator 1.Translate(ExpressionConverter parent, Expression linq) at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input) at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding) at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda) at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod) at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator
1.在System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)上的System.Data.Objects.ELinq.ExpressionConverter.Convert()中转换(ExpressionConverter parent,Expression linq)。 Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable 1 forMergeOption) at System.Data.Objects.ObjectQuery
1.GetResults(Nullable 1 forMergeOption) at System.Data.Objects.ObjectQuery
1.System.Collections.Generic.IEnumerable.GetEnumerator()at System.Collections.Generic.List {{1 System.Linq.Enumerable.ToList [TSource](IEnumerable`1 source)在E:\ Gharbarindia \ Dev \ GharbarIndia.Data \ User \ UserManager中的GharbarIndia.Data.User.UserManager.Agents()中的1个集合。 cs:位于E:\ Gharbarindia \ Dev \ GharbarIndia.ServiceCore \ RealAgent.cs:GharbarIndia.ServiceCore.RealAgent.GetAll()的第179行16在SyncInvokeGetAll(Object,Object [],Object [])处于System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance,Object [] inputs,Object []&amp; System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)位于System的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&amp; rpc)。 ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&安培; RPC)在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&安培; RPC)在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&安培; RPC)在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&amp; rpc)的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&amp; rpc)上的.ProcessMessage2(MessageRpc&amp; rpc),位于System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet) )
答案 0 :(得分:0)
您无法投影到实体。尝试创建一个只包含您需要选择的字段的新类。
public class MyRealUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string AboutProfession { get; set; }
}
var q = from x in DBContextHelper.DBContext.RealUsers
orderby x.CreatedOn descending
select new MyRealUser()
{
FirstName = x.FirstName,
LastName =x.LastName,
AboutProfession = x.AboutProfession
};