Josh Deshazer
Josh Deshazer

Reputation: 91

Having issues with ternary operator

I am trying to get back specific data from a ef query ,but I am having issues with my ternary operator

PlumbingSuperintendent = prj.JobInformation.Subdivision.AORs
.Where(x => 
    x.AOR_Contact_Type_ID == "SI" && x.Service_Type == "PLUMBING" && x.Effective_End_Date != null
    ? (x.Effective_End_Date > DateTime.Now)
    : !x.Effective_End_Date.HasValue && x.IsPrimary
)
.Select(x => x.Employee.Employee_FName + " " + x.Employee.Employee_LName).FirstOrDefault(),

what I am wanting to do if the effective end date is not null give me the records where effectice end date is greater than today. if not then give me the record with effective end date as null. For some reason this isn't working

Upvotes: 0

Views: 61

Answers (1)

Dave Anderson
Dave Anderson

Reputation: 12304

You can create this condition with the null coalescing operator.

(x.Effective_End_Date ?? DateTime.Now) >= DateTime.Now

You may need to assign DateTime.Now to a variable as using it twice like this may not have exactly the same value.

Upvotes: 3

Related Questions