Adding a conditional Where in a LINQ statement

  • Thread starter Thread starter guyinkalamazoo3
  • Start date Start date
G

guyinkalamazoo3

Guest
I have a filter in a UI that has start and end dates. Both of the datetimepickers default to the current date/time. I am setting two datetime variables, startdate and enddate, respectively, with the values of the controls. If the values match then no filtering needs to happen with the linq statement, but if they do not match then that means the user wants to filter the list based on either one of the dates and I need to add a where clause. I can do this by doing an If...then...else statement, but I don't want to have to write the same LINQ logic each time there is a filter. is there a way in VB to do this? Thanks and here is my code, which I know is not correct.

logList = (From l In pddb.UpdateLogs
If enddate <> startdate Then logList => Where l.UpdatedDate >= startdate And l.UpdatedDate <= enddate
Order By l.UpdatedDate Descending
Select New TheseLogFiles With {
.ID = l.UpdateID.ToString,
.ProcessType = l.ProcessType,
.UpdatedDate = l.UpdatedDate,
.UpdatedNote = l.UpdateNote,
.UpdatedBy = l.UpdatedBy})




Brad Allison

Continue reading...
 
Back
Top