first off, a disclaimer: i am new to Linq. still learning. i've used LLBLGen for years, and i'm excited to combine LL with the new Linq technology.
so. the problem is that in searches, i need to adjust date predicates so that they are actually 'between' filters that encompass all the DateTime values in a particular day. and i want this done in the emitted SQL, NOT in memory. i could do this with a lot of individual predicates, but i'd rather have a generic way to do it. the code for a sample of this is:
static bool AdjustedDateEquals(this DateTime thisVal, DateTime compareVal)
{
TimeSpan oneSecond = TimeSpan.FromSeconds(1f);
DateTime maxDate = compareVal.Date.AddDays(1f).Subtract(oneSecond);
DateTime minDate = compareVal.Date; //this will be midnight
return (thisVal >= minDate && thisVal <= maxDate);
}
now, i have a way to do this inside a Linq query when i know the particular Entity being filtered, and the field to filter on. it generates the correct SQL etc. the prototype looks like:
static IQueryable<MyParticularKindOfEntity> AdjustedDateCreatedEquals(this IQueryable<MyParticularKindOfEntity> sa, DateTime compareVal)
what i want is something vaguely like this:
internal static IQueryable<MyParticularKindOfEntity> AdjustedDateEquals(this IQueryable<MyParticularKindOfEntity> sa, DateTime compareVal, string fieldName)
however, my attempts to do this more generically have failed. the above extension method for DateTime fails with a Query Construction exception, and i can understand why. what i want to do is have a method that accepts an Entity Field and a Value (DateTime), and build a filter that can be added via IQueryable<MyEntity>.Where().
i know that there are simpler ways to do this, but given that i have a lot of different date fields, i don't want to duplicate the same code in many different places. i would greatly prefer to have a single "DateEquals" operator that i can use for all queries that have to use ThisDate=?
again, keeping in mind that i am still learning Linq (and lambda syntax), what's a good generic approach to take to this problem? i have similar problems to address with decimal measurement fields where i need to apply a 'tolerance' to an equality operator.
Thanks much.