I have create a restful service which returning a List an entity type:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class RealAgent : IRealAgent
{
public IList<RealUser> GetAll()
{
return UserManager.Agents();
}
}
//User Manager class implementation
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();
}
// I am fetching selected column. But it is not working. Do I need to create own class OR It is possible to implement with Entity Framework?
Error: The server encountered an error processing the request. The exception message is 'The entity or complex type 'GharbarIndia.Data.RealUser' cannot be constructed in a LINQ to Entities query.'. See server logs for more details. The exception stack trace is:
at System.Data.Objects.ELinq.ExpressionConverter.CheckInitializerType(Type type) at System.Data.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq) at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator1.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.Translate(ExpressionConverter parent, Expression linq) at System.Data.Objects.ELinq.ExpressionConverte开发者_如何学运维r.TranslateExpression(Expression linq) at System.Data.Objects.ELinq.ExpressionConverter.Convert() at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable1 forMergeOption) at System.Data.Objects.ObjectQuery
1.GetResults(Nullable1 forMergeOption) at System.Data.Objects.ObjectQuery
1.System.Collections.Generic.IEnumerable.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable
1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at GharbarIndia.Data.User.UserManager.Agents() in E:\Gharbarindia\Dev\GharbarIndia.Data\User\UserManager.cs:line 179 at GharbarIndia.ServiceCore.RealAgent.GetAll() in E:\Gharbarindia\Dev\GharbarIndia.ServiceCore\RealAgent.cs:line 16 at SyncInvokeGetAll(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
You cannot project onto an entity. Try creating a new class containing only the fields that you need to select.
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
};
精彩评论