Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Q] Range doesn't include null fields in database #22

Closed
enisn opened this issue Jan 11, 2021 · 1 comment
Closed

[Q] Range doesn't include null fields in database #22

enisn opened this issue Jan 11, 2021 · 1 comment
Assignees
Labels
question Further information is requested useful-tips Some useful tips & examples

Comments

@enisn
Copy link
Owner

enisn commented Jan 11, 2021

Problem

By default, Range<T> doesn't include null values, it generates only GTE (>=) and LTE (<=) comparison. So null values don't match these arithmetic comparison because they're not greater or lesser, they're just NULL.

@enisn enisn added question Further information is requested discussion labels Jan 11, 2021
@enisn enisn self-assigned this Jan 11, 2021
@enisn
Copy link
Owner Author

enisn commented Jan 11, 2021

By the way, I got some feedbacks about including nulls in range query. There is a way to include nulls in your query:

  • Overriding Range!

Following class will solve this problem. But it's not a best practise so it'll not be inlcuded in AutoFilterer library by default. You can create in your project and use if you really need.

using System;
using System.Linq.Expressions;
using System.Reflection;
using AutoFilterer.Types;

namespace AutoFilterer.Types
{
        public class RangeOrNull<T> : Range<T> where T : struct, IComparable
        {
            public override Expression BuildExpression(Expression body, PropertyInfo targetProperty, PropertyInfo filterProperty, object value)
            {
                var rangeComparison = base.BuildExpression(body, targetProperty, filterProperty, value);
                
                var nullComparison = Expression.Equal(Expression.Property(body, targetProperty.Name), Expression.Constant(null));

                return  Expression.Or(rangeComparison, nullComparison);
            }
        }
}
  • Then use RangeOrNull instead of Range
- public Range<DateTime> PublishDate { get; set; }
+ public RangeOrNull<DateTime> PublishDate { get; set; }

@enisn enisn added useful-tips Some useful tips & examples and removed discussion labels Jan 11, 2021
@enisn enisn changed the title Range doesn't include null fields in database [Q] Range doesn't include null fields in database Jan 14, 2021
@enisn enisn closed this as completed Jan 14, 2021
@enisn enisn pinned this issue Jan 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested useful-tips Some useful tips & examples
Projects
None yet
Development

No branches or pull requests

1 participant