12

How do I compare time only of DateTime Object without getting the following

Error:

An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code Additional information: The specified type member 'TimeOfDay' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

My code:

var date = DateTime.Parse(query.DueTime);
entities = entities.Where(r => r.DueTime.TimeOfDay.Equals(date.TimeOfDay));
eugenekgn
  • 1,652
  • 2
  • 17
  • 38

2 Answers2

20

This is the answer!

var date = DateTime.Parse(query.DueTime);
var time = date.TimeOfDay;
entities = entities.Where(r => DbFunctions.CreateTime(r.DueTime.Hour, r.DueTime.Minute,  r.DueTime.Second) == time);
Igor
  • 60,821
  • 10
  • 100
  • 175
eugenekgn
  • 1,652
  • 2
  • 17
  • 38
  • I am not getting DbFunctions . It gives me error "The name DbFunctions does not exist in current context." Do i have to use any namespace int it ? – Rohit Arora May 11 '15 at 04:18
-3

Have you tried using the function EntityFunctions.TruncateTime(TIME) ?

it would go as follows:

var date = DateTime.Parse(query.DueTime);
entities = entities.Where(r => EntityFunctions.TruncateTime(r.DueTime) == EntityFunctions.TruncateTime(date));

Hope this helps!

Gaston Claret
  • 1,000
  • 1
  • 7
  • 27
  • 8
    This would be used for comparing dates **ignoring time** , OP is asking for comparing time only – Habib Jan 22 '14 at 16:02