How to use BETWEEN keyword in Entity Framework?

Tharik Kanaka picture Tharik Kanaka · Aug 27, 2012 · Viewed 29.9k times · Source

How do i use BETWEEN key word in Entity Framework?

This is my SQL query

    SELECT *
  FROM [HRM].[dbo].[meals]
  WHERE SHIFTCODE = 'Normal'
  AND 12.59 BETWEEN [STAR TIME] AND [STAR TIME]+DURATION/10;

This is my Entity Framework Code.

HRMEntities HRM = new HRMEntities();
            meal ml = HRM.meals.SingleOrDefault(p => p.SHIFTCODE == "Normal");

value 12.59 can be a variable

Answer

Habib picture Habib · Aug 27, 2012

You can use >= and <= with DateTime to get a range.

HRMEntities HRM = new HRMEntities();
meal ml = HRM.meals.SingleOrDefault(p => p.SHIFTCODE == "Normal" 
                                    && p.StartTime <= yourTime 
                                    && (p.StartTime + duration) >= yourtime);