ZEXSM / OData.QueryBuilder

OData.QueryBuilder - library for creating complex OData queries (OData version 4.01) based on data models with linq syntax.
MIT License
75 stars 31 forks source link

Odata bracketing #20

Closed DolgsthrasirCGN closed 4 years ago

DolgsthrasirCGN commented 4 years ago
.Filter(s => s.IdRule == constValue
                    && s.IsActive
                    && (((DateTimeOffset)s.EndDate).Date == default(DateTimeOffset?) || s.EndDate > DateTime.Today)
                    && (((DateTime)s.BeginDate).Date != default(DateTime?) || ((DateTime)s.BeginDate).Date <= DateTime.Today)
                    && constStrIds.Contains(s.ODataKind.ODataCode.Code))

Generates following odata query.

$filter=IdRule eq 3 and IsActive and date(EndDate) eq null or EndDate gt {DateTime.Today.ToString("O")} and date(BeginDate) ne null or date(BeginDate) le {DateTime.Today.ToString("yyyy-MM-dd")} and ODataKind/ODataCode/Code in ('123','512')

All the bracketing of the lambda is not taken into account. It should be

$filter=IdRule eq 3 and IsActive and (date(EndDate) eq null or EndDate gt {DateTime.Today.ToString("O")}) and (date(BeginDate) ne null or date(BeginDate) le {DateTime.Today.ToString("yyyy-MM-dd")}) and ODataKind/ODataCode/Code in ('123','512')

ZEXSM commented 4 years ago

Hi, the current version unfortunately loses the brackets. I will try to do something

ZEXSM commented 4 years ago

Hi. I can suggest for the time being such an option to support brackets.

.Filter((s, f, o) => s.IdRule == constValue
    && s.IsActive
    && (f.Date(s.EndDate.Value) == default(DateTimeOffset?) || s.EndDate > DateTime.Today)
    && (f.Date((DateTimeOffset)s.BeginDate) != default(DateTime?) || f.Date((DateTime)s.BeginDate) <= DateTime.Now)
    && o.In(s.ODataKind.ODataCode.Code, constStrIds))

$filter=(((((IdRule eq 3) and IsActive) and ((date(EndDate) eq null) or (EndDate gt 2020-08-26T00:00:00Z))) and ((date(BeginDate) ne null) or (date(BeginDate) le 2020-08-26T16:29:36Z))) and ODataKind/ODataCode/Code in ('123','512'))

The main problem is that the parentheses are created automatically if this is a combined expression, and it is not easy to determine those that were added manually. In the future, I will be working on removing the automatically added parentheses from the uri.

ZEXSM commented 4 years ago

Removed unnecessary parentheses and added optional controls

.Filter((s, f, o) => s.IdRule == constValue
    && s.IsActive
    && (f.Date(s.EndDate.Value) == default(DateTimeOffset?) || s.EndDate > DateTime.Today)
    && (f.Date((DateTimeOffset)s.BeginDate) != default(DateTime?) || f.Date((DateTime)s.BeginDate) <= DateTime.Now)
    && o.In(s.ODataKind.ODataCode.Code, constStrIds), useParenthesis: true)

$filter=(((IdRule eq 3 and IsActive) and (date(EndDate) eq null or EndDate gt 2020-08-29T00:00:00Z)) and (date(BeginDate) ne null or date(BeginDate) le 2020-08-29T18:09:15Z)) and ODataKind/ODataCode/Code in ('123','512')