Hello and Happy New Year! The last article is about one library I develop that helps you build REST APIs with CRUD, filtering and many more features that enhance the development of applications in ASP.NET Core.
In this article I’ll be talking a bit about the filtering functionality that is implemented in the library. This is really helpful in many situations and it’s very efficiently implemented so that SQL statements are ran directly in the database and not in the memory.
How to use them
Good, let’s give it a bit of context. Let’s suppose that we have an endpoint which returns paged data. I use this article as a starting point. We’ll send GET requests to the endpoint and every query statement will be added as a parameter in the query string. Each statement has the form:
As you already know, each query parameter should be separated by ampersand (&). Multiple statements are considered separated by an AND logical operator. If there are two identical operations that refer to the same member, between them OR is applied.
Here is a list of filters that can be applied. You can see them like a kind of OData but simpler in my opinion.
- eq – equal operator which works with int, float, Guid, string and doubles. As of this writing, it seems that bool is not working 🙄
- neq – not equal operator which works as above
- gt – greater than operator which works with comparable types
- gte – greater than or equal operator
- lt – less than operator which works with comparable types
- lte – less than or equal
- contains – only for strings, checks if the value is contained in the string
- starts – only for strings, checks if the member value starts with value
- ends – only for strings, checks if the member value ends with value
Note that the contains, starts and ends operations are case sensitive. A plan to add case insensitive operations for these exists.
Putting into practice
Let’s use a couple of operations so you can see in practice how they work. Let’s suppose a normal GET request looks like this:
The paginated response returns all the entities available (in our case 3). Let’s try to filter only the products that have the price equal to 2.899:
As you can see, the call returned 2 results now. Ok let’s try the not equal operation:
Pretty cool, no? Let’s see greater than + less than equal operations:
In this case, we got only one product respecting the conditions. Let’s also see a starts operation:
I hope this article showed in an easy way how to use the filter operations in your controller. You get this and many others out of the box without writing code if you follow the first article dedicated to the Mindgaze.AspNetCore library. As a proof, you can see I didn’t use any code in this article and you’ll see that very little to no code is required to benefit from the features it provides.
This Mindgaze.AspNetCore library has more features like:
- Testing with fixtures
- DTO supplier
I will write articles about all of these because I think they are really useful in the context of a production application. So if you have questions let me know, I hope you enjoyed this!
Thanks for reading, I hope you found this article useful and interesting. If you have any suggestions don’t hesitate to contact me. I also invite you to share and subscribe to the newsletter by using the buttons below! Cheers 😉