C# ';等待&x27;运算符只能在异步lambda表达式中使用。考虑用λx27标记λ表达式;异步';修饰语
在下面的LinQ查询中,我调用了另一个异步方法C# ';等待&x27;运算符只能在异步lambda表达式中使用。考虑用λx27标记λ表达式;异步';修饰语,c#,entity-framework,linq,C#,Entity Framework,Linq,在下面的LinQ查询中,我调用了另一个异步方法getAspNetUserPhoneNumberByAccount,它抛出了这个错误 错误CS4034“await”运算符只能在异步lambda表达式中使用。考虑用“AsiNC”修饰符标记lambda表达式。 有人知道吗 var fullAppointment = await Task.Run(() => Context.AppointmentDetail .Where(u => u.StartDateTime &g
getAspNetUserPhoneNumberByAccount
,它抛出了这个错误
错误CS4034“await”运算符只能在异步lambda表达式中使用。考虑用“AsiNC”修饰符标记lambda表达式。
有人知道吗
var fullAppointment = await Task.Run(() => Context.AppointmentDetail
.Where(u =>
u.StartDateTime >= startdatetime
&& u.EndDateTime <= enddatetime
)
.Select(x => new Contracts.CalenderModel2()
{
StatusId = (Contracts.Enum.EnumWOStatus)x.Status,
FName = x.Appointment != null ? x.Appointment.Customer.Account.FName : "",
LName = x.Appointment != null ? x.Appointment.Customer.Account.LName : "",
**PrimaryPhone = x.Appointment != null ?
(await _userRepository.GetAspNetUserPhoneNumberByAccountId( x.Appointment.Customer.AccountId))**
: "",
Year = x.Appointment != null && x.Appointment.Vehicle != null ? x.Appointment.Vehicle.MakeYear.Year : 0,
Make = x.Appointment != null && x.Appointment.Vehicle != null ? x.Appointment.Vehicle.VehicleMaker.MakerName : "",
Model = x.Appointment != null && x.Appointment.Vehicle != null ? x.Appointment.Vehicle.VehicleModel.Model : "",
AppointmentId = x.AppointmentId,
JobEndDateTime = x.EndDateTime,
JobStartDateTime = x.StartDateTime,
ColorCategory = x.AppointmentType.ColorCategory,
SalesRepersentativeUserId =
(x.Appointment != null && x.Appointment.Customer.CustomerBillTo.Count > 0)
? x.Appointment.Customer.CustomerBillTo.FirstOrDefault().BillToId : Guid.Empty,
FullAppointmentDetail = new Contracts.FullAppointmentDetail
{
BayId = x.BayId,
BayName = x.WorkArea != null ? x.WorkArea.BayName : "",
WorkTypeId = x.WorkTypeId,
WorkTypeName = x.WorkType != null ? x.WorkType.WorkTypeName : "",
JobId = x.Appointment != null && x.Appointment.Job != null ? x.Appointment.Job.Id : Guid.Empty,
JobIdInt = x.Appointment != null && x.Appointment.Job != null ? x.Appointment.Job.JobIdInt : 0,
AssigneeUserId = x.AssigneeUserId,
WorkOrderId = x.WorkOrders.FirstOrDefault() != null ? x.WorkOrders.FirstOrDefault().Id : Guid.Empty
}
})
.ToList());
var fullAppointment=wait Task.Run(()=>Context.AppointmentDetail
.其中(u=>
u、 StartDateTime>=StartDateTime
&&u.EndDateTime新合同。日历模型2()
{
StatusId=(Contracts.Enum.EnumWOStatus)x.Status,
FName=x.约会!=null?x.Appointment.Customer.Account.FName:“”,
LName=x.Appointment!=null?x.Appointment.Customer.Account.LName:“”,
**PrimaryPhone=x.约会!=null?
(wait_userRepository.GetAspNetUserPhoneNumberByAccountId(x.Appointment.Customer.AccountId))**
: "",
年份=x.约会!=空和&x.Appointment.Vehicle!=空?x.Appointment.Vehicle.MakeYear.Year:0,
Make=x.约会!=null&&x.Appointment.Vehicle!=null?x.Appointment.Vehicle.VehicleMaker.MakerName:“”,
Model=x.Appointment!=null&&x.Appointment.Vehicle!=null?x.Appointment.Vehicle.VehicleModel.Model:“”,
AppointmentId=x.AppointmentId,
JobEndDateTime=x.EndDateTime,
JobStartDateTime=x.StartDateTime,
ColorCategory=x.AppointType.ColorCategory,
SalesReservativeUserid=
(x.Appointment!=null&&x.Appointment.Customer.CustomerBillTo.Count>0)
?x.Appointment.Customer.CustomerBillTo.FirstOrDefault().BillToId:Guid.Empty,
FullAppointDetail=新合同。FullAppointDetail
{
BayId=x.BayId,
BayName=x.WorkArea!=null?x.WorkArea.BayName:“”,
WorkTypeId=x.WorkTypeId,
WorkTypeName=x.WorkType!=null?x.WorkType.WorkTypeName:“”,
JobId=x.Appointment!=null&&x.Appointment.Job!=null?x.Appointment.Job.Id:Guid.Empty,
jobidit=x.Appointment!=null&&x.Appointment.Job!=null?x.Appointment.Job.jobidit:0,
AssigneeUserId=x.AssigneeUserId,
WorkOrderId=x.WorkOrders.FirstOrDefault()!=null?x.WorkOrders.FirstOrDefault().Id:Guid.Empty
}
})
.ToList());
下面给出了GetAspNetUserPhoneNumberByAccount的定义
public async Task<string> GetAspNetUserPhoneNumberByAccountId(Guid accountId)
{
var phone = await Task.Run(() => _Context.Account.Where(ac => ac.Id.Equals(accountId))
.Join(_Context.AspNetUsers, ac => ac.AspNetUserId, u => u.Id, (ac, u) => new
{
PhoneNumber = u.PhoneNumber,
}).FirstOrDefault());
return phone!=null?phone.ToString():"";
}
公共异步任务GetAspNetUserPhoneNumberByAccountId(Guid accountId)
{
var phone=wait Task.Run(()=>_Context.Account.Where(ac=>ac.Id.Equals(accountId))
.Join(_Context.AspNetUsers,ac=>ac.AspNetUserId,u=>u.Id,(ac,u)=>new
{
PhoneNumber=u.PhoneNumber,
}).FirstOrDefault());
返回phone!=null?phone.ToString():“”;
}
我同意各位的意见,在这个查询中不需要使用异步方法。此外,此查询不会进行优化。对于Where条件下的每个项目,您将向DB查询它。如果有大量数据,数据库的性能会下降。我认为在这种情况下,最好对DB执行2次查询。用于获取AppointmentDetail
和另一个用于获取所有帐户的查询。这两个查询调用都是异步的。然后将其合并到您的合同中。CalenderModel2
类
它将更具可读性,工作速度更快。
并且不要使用Task.Run() 为什么要使用
Task.Run
?关于具体问题,这个网站上有。但是我不认为您首先应该在这里使用async
。为什么需要等待?该方法不是异步的,因此它将自动等待。除了关于为什么要使用任务的问题之外,请运行并等待。只需调用Task.Run(异步()=>{…
应该去掉这个error@Knoop我认为您实际上也需要在Select
中向lambda添加async
。然后您会得到一个返回IEnumerable
的查询,您必须使用Task.whalll
打开。@CharlesMager您是对的。所有包含等待操作的匿名函数都应该是完成时,看起来是这样的:.Select(async x=>…
我知道了,谢谢你的评论。