gpt4 book ai didi

jquery - linq to sql 列表与 Restful 服务中的选定列

转载 作者:行者123 更新时间:2023-12-01 06:04:36 24 4
gpt4 key购买 nike

我创建了一个返回列表实体类型的 Restful 服务:

    [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();
}

//我正在获取选定的列。但它不起作用。我需要创建自己的类还是可以使用 Entity Framework 来实现?

错误:
服务器在处理请求时遇到错误。异常消息是“无法在 LINQ to Entities 查询中构造实体或复杂类型 'GharbarIndia.Data.RealUser'。”。有关更多详细信息,请参阅服务器日志。异常堆栈跟踪是:

在 System.Data.Objects.ELinq.ExpressionConverter.CheckInitializerType(Type type) 在 System.Data.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq) 在 System.Data.Objects.ELinq.ExpressionConverter.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.Translate(ExpressionConverter parent, Expression linq) at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Objects.ELinq.ExpressionConverter.Convert() at System.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..ctor(IEnumerable 1 个集合) 在 System.Linq.Enumerable.ToList[TSource](IEnumerable` 1 来源)在 E:\Gharbarindia\Dev\GharbarIndia.Data\User\UserManager.cs 中的 GharbarIndia.Data.User.UserManager.Agents():E:\Gharbarindia 中 GharbarIndia.ServiceCore.RealAgent.GetAll() 的第 179 行\Dev\GharbarIndia.S erviceCore\RealAgent.cs:第 16 行,位于 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) 在 System.ServiceModel 的 SyncInvokeGetAll(Object, Object[] , Object[]) .Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher .ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMess System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet) 处的 age1(MessageRpc& rpc)

最佳答案

您不能投影到实体上。尝试创建一个只包含您需要选择的字段的新类。

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
};

关于jquery - linq to sql 列表与 Restful 服务中的选定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7726928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com