views:

347

answers:

1

Hi All,

I have a linq Entity called Enquiry, which has a property: string DateSubmitted.

I'm writing an app where I need to return IQueryable for Enquiry that have a DateSubmitted within a particular date range.

Ideally I'd like to write something like

IQueryable<Enquiry> query = Context.EnquirySet.AsQueryable<Enquiry>();
int dateStart = int.Parse("20090729");
int dateEnd = int.Parse("20090930");

query = (from e in query
     where(enq => int.Parse(enq.DateSubmitted) < dateEnd)
     where(enq => int.Parse(enq.DateSubmitted) > dateStart)
     select e);

Obviously Linq to EF doesn't recognise int.Parse, so I think I can achieve what I want with an Expression method that returns a predicate???

I've been playing around with PredicateBuilder and looking all over but I've successfully fried my brains trying to work this out. Sure I could add another property to my Entity and convert it there but I'd really like to understand this. Can anyone explain or give an example/link that doesn't fry my brains?

Thanks in advance

Mark

+2  A: 

If you know your date strings are valid, and they're really in that order (which is a natural sort order) you might be able to get away with string comparisons:

IQueryable<Enquiry> query = Context.EnquirySet.AsQueryable<Enquiry>();
string dateStart ="20090729";
string dateEnd = "20090930";

query = (from e in query
     where(enq => enq.DateSubmitted.CompareTo(dateEnd)) < 0)
     where(enq => enq.DateSubmitted.CompareTo(dateStart)) > 0)
     select e);
Jon Skeet
That's a great workaround, thanks I shall probably use that, thanks!!!For my knowledge though is there a way of doing this sort of comparison with a Predicate? I'm slowly getting my head around this :-)
Mark
Well you were doing it with a predicate to start with. I'm not sure I really understand the question...
Jon Skeet
JonI think I was looking for a more generic way of being able to compare values when a type conversion was required. I have situations where Entity properties are exposed as Enums and strings need to be compared to int. I thought from looking at PredicateBuilder I might be able to write a generic set of methods that could for instance to do a less than or greater than comparison converting the input Types to the required type.Please excuse my lack of knowledge, I understand thr basics of Linq but i'm trying to get my head around extensions methods, Expresison trees and Predicates.
Mark