2

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.

canton7
  • 37,633
  • 3
  • 64
  • 77
user2155362
  • 1,657
  • 5
  • 18
  • 30
  • 2
    Have you tried anything? – Abion47 Feb 07 '19 at 06:52
  • Can you post more code and more description to the question? – Bijay Yadav Feb 07 '19 at 06:56
  • What's the problem of using `>=`? It seems to be working fine in [this example](https://dotnetfiddle.net/U2XKgu) – ikerbera Feb 07 '19 at 06:58
  • 2
    Possibly [Expression.GreaterThan fails if one operand is nullable type, other is non-nullable](https://stackoverflow.com/q/2088231) or [Building LINQ to Entities Expression tree with DateTime filter](https://stackoverflow.com/q/43649673) apply. – dbc Feb 07 '19 at 07:04
  • See IComparable, https://learn.microsoft.com/en-us/dotnet/api/system.icomparable – Immersive Feb 07 '19 at 07:19
  • 2
    This is beginning to sound like an [XY problem](http://xyproblem.info/). What is the purpose of what you're trying to do? – Abion47 Feb 07 '19 at 07:20
  • 1
    OK, your question seems clearer now. But perhaps the answer to [Expression.GreaterThan fails if one operand is nullable type, other is non-nullable](https://stackoverflow.com/q/2088231) answers this also? – dbc Feb 07 '19 at 07:56
  • 1
    Possible duplicate of [Expression.GreaterThan fails if one operand is nullable type, other is non-nullable](https://stackoverflow.com/questions/2088231/expression-greaterthan-fails-if-one-operand-is-nullable-type-other-is-non-nulla) – canton7 Mar 20 '19 at 09:12

0 Answers0