Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ';等待&x27;运算符只能在异步lambda表达式中使用。考虑用λx27标记λ表达式;异步';修饰语_C#_Entity Framework_Linq - Fatal编程技术网

C# ';等待&x27;运算符只能在异步lambda表达式中使用。考虑用λx27标记λ表达式;异步';修饰语

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

在下面的LinQ查询中,我调用了另一个异步方法
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=>…
我知道了,谢谢你的评论。