ZEXSM / OData.QueryBuilder

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

Calling `Filter` multiple times with `useParenthesis` produces unexpected results #97

Open crackalak opened 2 years ago

crackalak commented 2 years ago
class Test
{
    public string Name { get; set; }
    public string Description { get; set; }
    public int Value { get; set; }
}
var query1 = queryBuilder.For<Test>("mock")
                         .ByList()
                         .Filter((x, f, o) => (x.Name == "name" || x.Description == "description") && x.Value == 1, useParenthesis: true);
var items1 = query1.ToDictionary();
var filter1 = items1["$filter"];

Output (Correct): (Name eq 'name' or Description eq 'description') and Value eq 1

var query2 = queryBuilder.For<Test>("mock")
                         .ByList()
                         .Filter((x, f, o) => (x.Name == "name" || x.Description == "description"), useParenthesis: true);
query2 = query2.Filter((x, f, o) => x.Value == 1, useParenthesis: true);
var items2 = query2.ToDictionary();
var filter2 = items2["$filter"];

Output (Incorrect): Name eq 'name' or Description eq 'description' and Value eq 1

I would expect both outputs to match, should this not be the case?

amorelIM commented 11 months ago

I'm a bit late, but I found a way to patch the problem while waiting for real fix. It can be done by placing a "true" in front of your conditions.

var query2 = queryBuilder.For<Test>("mock")
                         .ByList()
                         .Filter((x, f, o) => true && (x.Name == "name" || x.Description == "description"), useParenthesis: true);
query2 = query2.Filter((x, f, o) => x.Value == 1, useParenthesis: true);

var items2 = query2.ToDictionary();

var filter2 = items2["$filter"];
ronaldoasilva commented 2 months ago

I'm a bit late, but I found a way to patch the problem while waiting for real fix. It can be done by placing a "true" in front of your conditions.

var query2 = queryBuilder.For<Test>("mock")
                         .ByList()
                         .Filter((x, f, o) => true && (x.Name == "name" || x.Description == "description"), useParenthesis: true);
query2 = query2.Filter((x, f, o) => x.Value == 1, useParenthesis: true);

var items2 = query2.ToDictionary();

var filter2 = items2["$filter"];

it worked for me, tthanks man :)