ZEXSM / OData.QueryBuilder

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

Filter treats decimal values as strings #103

Closed michaelgr1 closed 1 year ago

michaelgr1 commented 2 years ago

When the query builder is used to filter on decimal values, the returned URI contains the decimal values but in quotes. It seems like they are not recognized as numbers, but rather as strings. Consider the following example:

        public static void Main(string[] args) {
            var builder = new ODataQueryBuilder("/odata").For<Item>("Items").ByList();
            decimal decimalValue = 10;
            double doubleValue = 20;
            builder.Filter(p => p.Price == decimalValue);
            Console.WriteLine(builder.ToUri());
        }

        public class Item {
            public decimal Price { get; set; }

            public double Id { get; set; }
        }

Running the code above I get this output: /odata/Items?$filter=Price eq '10' Expected output is: /odata/Items?$filter=Price eq 10

Running the same code but with a literal value produces the same result. Using the Id property on the other hand, with the following code: builder.Filter(p => p.Id == 2); gives the expected output of: /odata/Items?$filter=Id eq 2

For the decimal property, the same behavior was observed for other operators like >=, <=. I managed to work around the issue by using a double variable for the filter, but converting it inline to decimal, as shown in the code bellow:

double doubleValue = 20;
builder.Filter(p => p.Price == (decimal)doubleValue);

This gives the expected output: /odata/Items?$filter=Price eq 20

I'd love to provide any more details and help out. I've been using this library for the past couple of months and it makes my life so much easier as most of my API is based on OData. I'm running the latest version (2.9.6) and use it in a Blazor WebAssembly project.

Thanks for your efforts, Michael.

ZEXSM commented 1 year ago

Thanks for the detailed question, it helps to find and solve the problem faster. The current issue has been fixed in #104 and will be published soon.

michaelgr1 commented 1 year ago

Thanks for the quick response and fix, I can confirm everything works as expected after updating to the latest version 👍