user2739418
user2739418

Reputation: 1631

c# Linq/DBContext Query between dates or greater or lower depend of parameters passed

I have to filter data based on Dates passed Parameters: StartDate, EndDate with default value null.

Filter:

  1. If both parameters are null then return all records
  2. If Start Date passed but end date not then return all record after start date
  3. If EndDate passed but not start date then return all rows before EndDate
  4. If both Dates passed then return record between dates.

    var appointmentNoShow = _db.Appointments Where (x=> s.appointmentDate ?? ).ToList();

So I need separate queries or I can do with some condition in one query.

Upvotes: 0

Views: 259

Answers (1)

mathis1337
mathis1337

Reputation: 1619

You'd want to write a series of if statements to handle this.

if(StartDate == SomeDate && EndDate == Somdate)
{
return ALL Records after start date but before enddate
}
else if(StartDate == SomeDate && EndDate == Null)
{
return all records after StartDate
}
else if(StartDate == null && EndDate == SomeDate )
{
return all records before EndDate
}
else
{
Return all records 
}

this is the most basic way to represent this. You can of course clean this up and use conditionals in a different way, but this is about as basic an example you can get, so feel free to tweak it anyway you want.

Upvotes: 0

Related Questions