实体框架查询字段的条件

时间:2013-03-19 11:08:09

标签: c#-4.0 ef-code-first entity-framework-4.1

  var employeePayDetails = (from p in Catalog.Providers
                           where p.Key == providerKey
                           from a in p.Appointments
                           from e in a.Employee.Services
                           from l in a.Allocations
                           from ip in l.InvoicePayments
                           where (a.Employee != null && a.Created >= begin && a.Created <= end)                        where (e.Service.Id==l.Service.Id)
                           select new 
                           {
                           ServiceName = l.Service,
                           CustomersServiced = l.Pet.Name + " " + a.Owner.LastName,
                           AppointmentDate = a.Created,
                           EmployeeName = a.Employee.Name,
                           PaymentOptionType = e.PaymentOption,
                           RateValue = e.Rate,
                           WorkedHours=l.End-l.Start,
                           PayAmount= ?

                          }).ToList();

在上面的Entity框架查询中,我需要根据下面提到的switch语句计算 PayAmount

switch (PaymentOption)
        {
           case 1:
                PayAmount= WorkedHours * RateValue  ;
                break;
            case 2:
                PayAmount = RateValue  ;
                break;
            case 3:
               PayAmount = LatestTotal  ;
                break;
        }

那么我怎样才能将实体框架查询的上述switch语句值作为 PayAmount

1 个答案:

答案 0 :(得分:2)

标准if应该有效:

PayAmount= (e.PaymentOption == 1 ? (l.End-l.Start) * e.Rate : (e.PaymentOption == 2 ? e.Rate : LatestTotal ))