i have daterange class i'd apply iqueryable predicate, automatically using begin , end dates , automatically using open or closed interval.
public class daterange { public datetime? begindate { get; set; } public datetime? enddate { get; set; } public bool begininclusive { get; set; } public bool endinclusive { get; set; } public daterange() { begininclusive = true; endinclusive = false; } public iqueryable<t> apply<t>( iqueryable<t> source, expression<func<t,datetime>> datefield ) { var result = source; if (begindate.hasvalue) { if (begininclusive) result = result.where( x => datefield >= begindate ); //does not compile else result = result.where( x => datefield > begindate ); //does not compile } if (enddate.hasvalue) { if (endinclusive) result = result.where( x => datefield <= enddate ); //does not compile else result = result.where( x => datefield < enddate ); //does not compile } return result; } }
and want call this, datefield datetime property of t.
daterange d; iqueryable<t> q; q = d.apply( q, x => x.datefield );
so want pass member expression apply method, , have apply appropriate clause result set, cannot figure out how datefield member expression embedded in where predicate's expression. see lines "do not compile" in class above. need transform datefield somehow or build predicate expression other way, have no idea how so.
you'll have hand-craft datefield >= begindate
using expression class methods.
(...) if (begininclusive) { var greaterorequal = expression.lambda<func<t, bool>>( expression.greaterthanorequal( datefield.body, expression.constant(begindate)), datefield.parameters); result = result.where(greaterorequal); } (...)
similarly other cases.
Comments
Post a Comment