My question is how can I build lambda c=> c.docdate >= DateTime.Today
by Expression.GreaterThanOrEqual method.
Thanks
ps:
The purpose I do such things is I think I can apply such way to any query condition, such as int, decimal, . Then I can write any query condition by generic method and bind them together in any way.
for example:
I have a class like below:
public class DataRangeT<T> : IRangeValue<T>
where T : struct
{
Nullable<T> _high;
Nullable<T> _low;
public Nullable<T> High { get { return _high; } set { _high = value; } }
public Nullable<T> Low { get { return _low; } set { _low = value; } }
}
Beacuse the High and Low is Nullable, so I should build my query like below
public static Expression<Func<T, bool>> RangeCompare<T>(Expression<Func<T, K>> selector, IRangeValue<K> patten)
{
Expression<Func<T, bool>> predicate = f=>true;
if (patten.High.HasValue)
{
predicate = predicate.And<T>(/* LowThanOrEqual */);
}
if (patten.Low.HasValue)
{
predicate = predicate.And<T>(/* GreaterThanOrEqual */);
}
return predicate;
}
Because >= operator is not support in Nullable, then I think I should use GreaterThanOrEqual.
The method predicate.And<T>()
is a help method I write to combine query dynamically.