views:

231

answers:

2

If given a 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?

+1  A: 

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.

Nix
I like this. While there may be issues with scaling, this will work. SP way is probably the way to go.
LB
Completely agreed, but the first battle would be to change the DB type to date time!
Nix
And just as an fyi the code above can be simplified into one query.... by just doing var filtered = (select item from entities.itemsSet.AsEnumarable()
Nix
I can't change the DB type to time because the "client" has it that way =)
LB
And we only have readonly access to it, as they aren't going to change their schema.
LB
After the in-memory filtering, how do you convert back to LINQtoEntities? Since the .ToList() basically just converted to LINQtoObjects.
LB
Not sure i understand your question? They will still be entities, when i said turn them into objects i mean go ahead and select them from the database, then filter.
Nix
A: 

I was afraid this was the answer. I'm a little new to linq - what kind of performance implications are we talking? for a table say of 10k records?

Theres also a workaround using the CAST in a where statement, but I haven't figured out how to use it in my situation:

http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/abc19619-61ff-4bc5-bda9-df501f113c74

cheers

rusty

rusty