Search code examples
c#-4.0ef-code-firstentity-framework-4.1

Condition for Entity Framework Query Field


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

On above Entity framework query I need to calculate PayAmount based on below mentioned switch statement.

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

So how could I put above switch statement value for the entity framework query as the PayAmount ?


Solution

  • Standard if should work:

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