c# - 'await' 运算符只能在异步 lambda 表达式中使用。考虑用 'async' 修饰符标记这个 lambda 表达式
问题描述
在以下 LinQ 查询以获取电话号码时,我正在调用另一个异步方法 GetAspNetUserPhoneNumberByAccountId
,这会引发此错误
错误 CS4034 'await' 运算符只能在异步 lambda 表达式中使用。考虑用 'async' 修饰符标记这个 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());
GetAspNetUserPhoneNumberByAccountId 的定义如下
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():"";
}
解决方案
我同意评论中的人的观点,即您不需要在此查询中使用异步方法。此外,此查询不会优化。对于 Where 条件中的每个项目,您将查询到 DB。如果你有很多数据,数据库的性能会下降。我认为在这种情况下对数据库进行 2 次查询要好得多。获取AppointmentDetail
和获取所有帐户的另一个查询。并且两个查询调用都是异步的。然后将它结合到你的Contracts.CalenderModel2
课程中。
它将更具可读性并且工作得更快。并且不要使用 Task.Run();
推荐阅读
- asp.net-core - 如何将选项选择异步发布到 Microsoft Teams?
- arm - 使用带有 -L 参数的 QEMU 用户模式从 IoT 固件运行 ARM 二进制文件时出现问题
- mysql - 将三个 MySQL 查询合并为一个以使其更快
- php - 未定义的偏移量:laravel 6 中的 1
- python - 在 EMR 上运行 sparknlp DocumentAssembler
- python - DateTime 从多个 csv 文件中解析
- flutter - 如何将 100.00 转换为 100 和 100.23 转换为 100.23 飞镖
- laravel - Laravel 集合 AND WHERE 子句
- reactjs - ControlledEditor 中的 monaco-editor/react addCommand()
- gmail-api - 使用gmail api发送带有threadid的消息时如何归档邮件?