-you can use the timeofday property on the dates to compare them.

string timeasstring = "13:00";

from f in tablerows
where > datetime.parse("11-12-2012 "+timeasstring).timeofday
select f


here is some code you can test that runs for me:

  datetime d = datetime.parse("12-12-2012 13:00");
  list<datetime> dates = new list<datetime>();
  dates.add(datetime.parse("12-12-2012 13:54"));
  dates.add(datetime.parse("12-12-2012 12:55"));
  dates.add(datetime.parse("12-12-2012 11:34"));
  dates.add(datetime.parse("12-12-2012 14:53"));

  var result = (from f in dates where f.timeofday > d.timeofday select f);

edit 2

yea it seems that you needed to .tolist(), which, tbh you should have been able to figure out. i had no way of knowing what of collection you had. not sure either of us deserve a downvote for trying to help you when you don't supply an awful amount of information on the problem


if you had a date and a time column in your table (as possible with sql server 2008), you could do this directly in sql.

as that's not the case, you have to do it like this:

// the first two lines can be omitted, if your bounds are already timespans
var start = startdate.timeofday;
var end = enddate.timeofday;
var filtereditems = context.items.tolist()
    .where(x => x.datetimecolumn.timeofday >= start
        && x.datetimecolumn.timeofday <= end);


if you convert the date value to a double and use the fractional part of if, you get a number between 0 and 1 that represents a time in a day. having that, it is trivial to test for time intervals, where e.g. 13:45 would be 0,5729166667 (or more precise: 13:45.345 is 0,572920679).

you can do this because ef (i.e. 4.3, the version i use) translates cast and even math functions into sql: => dt.datetime.hasvalue)
    .select(dt => dt.datetime.value).cast<double>()
    .select(d => d -  math.floor(d))
    .where(... your comparisons

this translates to sql containing cast([date column] as float) and sql's floor function.

after your comments:

it looked so easy, but i can't find a way to instruct ef to do a cast on a single property in a select(). only the cast<>() function is translated to cast (sql), but that operates on a set.

well, fortunately, there is another way: => dt.datetime.hasvalue)
.select(dt => new
      date = dbfunctions.truncatetime(dt.datetime.value),
      ms = dbfunctions.diffmilliseconds(
               dbfunctions.truncatetime(dt.datetime.value), dt.datetime.value)
.where(x => > lower && < upper)

where lower and upper are the totalmilliseconds property of timespan objects.

note that this is horribly inefficient in sql! the date functions are repeated for each comparison. so make sure that you do the comparisons on a set of data that has been confined as much as possible by other criteria. it may even be better to fetch data in memory first and then do the comparisons.

note: prior to ef6, dbfunctions was entityfunctions.


var filteredtimes = mycontext.mytable
   .where(r => sqlfunctions.datepart("hour", r.datefield) >= 11 &&
               sqlfunctions.datepart("hour", r.datefield) <= 13);

you need to include to get sqlfunctions.

Related Query

More Query from same tag