Search code examples
linqdatetimelinq-to-entities

Linq to Entities DateTime Conversion


If given an entity with a DateTime as a string, what are my options to filter the data with LINQ to Entities on the date?

It does not seem to support me doing DateTime conversions.

Basically, I want to accomplish:

var filtered = from item in entities.itemsSet
               where Convert.ToDateTime(shift.starttime) >= startDate 
                   && Convert.ToDateTime(shift.endtime) < endDate
               select item;

What are my options to accomplish this?


Solution

  • You are going to end up doing in memory filtering.

    //So first select your data
    
    var data= (from item in entities.itemsSet).ToList();
    
    
    //Then filter
    var filtered = from item in data
               where Convert.ToDateTime(shift.starttime) >= startDate 
                   && Convert.ToDateTime(shift.endtime) < endDate
               select item;
    

    Another option would be to create a stored procedure to do it for you. In the SP you would have to take start/end and then compare it to your date time strings casted as Date Times.