Michel
Michel

Reputation: 23615

Linq query on the maximum value of 2 fields

i think the title is a little cryptic, here's what i want to do...

i have in my tabel two datetime fields. Now i want to select the rows where the largest (ie the most far in the future) date is larger than 'today'

few examples: (today is 6-22)
date1: null, date2: null : no match, all lower than now
date1: null, date2: 5-31: no match, all lower than now
date1: null, date2: 6-23: match, 6-23 is larger than now
date1: 5-31, date2: 7-23: match, 6-23 is larger than now
date1: 7-21, date2: 1-23: match, 7-21 is larger than now
date1: 7-21, date2: null: match, 7-21 is larger than now

so in sort of pseudo code:

select * from table where (max(date2, date2)) > now

Regards, Michel

Upvotes: 0

Views: 326

Answers (1)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421988

Is this what you want (I converted your SQL):

from row in table
where (row.Date1 != null && row.Date1 > DateTime.Now) || 
      (row.Date2 != null && row.Date2 > DateTime.Now)
select row

Upvotes: 5

Related Questions