Filtering In Your ASP NET Core Web API: The Key to Efficient Data Retrieval

Photo by Tyler Nix on Unsplash

Filtering In Your ASP NET Core Web API: The Key to Efficient Data Retrieval

Hello guys, I’m so glad to be here one more day to bring you some more valuable knowledge. Today we will not have API Friday 🥲, I know it’s sad but something it takes me more time than expected to build the projects and to play around with the new API every week at the same time that I’m creating content for the blog and some other daily life stuff. But here is the last API Friday blog post just in case you want to give it a check.

However today we will continue with our web API development series as we are getting our hands on some fun stuff, on the last article we were talking about pagination and its benefits for our application and the user experience, and today we will continue with a pretty similar topic as it is related. We will be talking about Filtering, why should you apply filtering, and as always a code example to continue to improve our Blog API so without further ado let’s get into today’s article.

On a Data-Driven World 🌍

As the world becomes increasingly connected and data-driven, web APIs have become a critical component of modern applications. APIs, or Application Programming Interfaces, allow developers to interact with the data and functionality of other software systems, enabling them to build more powerful, integrated applications.

One important feature of many web APIs is filtering. Filtering refers to the process of selecting a subset of data from a larger dataset based on specific criteria. In the context of a web API, filtering allows developers to retrieve only the data they need, rather than having to retrieve and process large amounts of irrelevant data.

What is filtering❓

Filtering is a process used to extract a subset of data from a larger dataset based on specific criteria. For example, if you were looking at a dataset of customer orders, you might want to filter the data to only show orders that were placed in the last month or orders that contained a specific product.

In a web API, filtering is typically accomplished by passing parameters to an API endpoint that specifies the criteria for selecting data. For example, you might use a parameter like ?filter=last_month to retrieve only the data that meets the specified criteria.

Filtering can be applied to various types of data, including text, numbers, dates, and more. The specific criteria used for filtering depends on the nature of the data and the requirements of the application.

Why is filtering important ❗

Filtering is an important feature in web APIs for several reasons:

Reduced data transfer 🔁

One of the primary benefits of filtering is reduced data transfer. When working with large datasets, retrieving all the data can be a slow and resource-intensive process. By filtering the data to only retrieve the necessary subset, developers can significantly reduce the amount of data that needs to be transferred and processed.

For example, if you're building an e-commerce application that needs to retrieve a list of products for sale, you might use filtering to retrieve only the products that are currently in stock. This would reduce the amount of data that needs to be transferred, making the application faster and more efficient.

Improved performance ⏩

Filtering can also improve the performance of web APIs by reducing the amount of data that needs to be processed. When retrieving data from a database or other data source, filtering can allow the API to query only the relevant subset of data, rather than retrieving and processing the entire dataset.

For example, if you're building a social media application that needs to retrieve a list of posts from a user's friends, you might use filtering to only retrieve posts that were created in the last week. This would reduce the amount of data that needs to be processed, making the API faster and more responsive.

Increased flexibility 🤸🏾

Filtering also provides increased flexibility for developers. By allowing developers to select only the data they need, APIs can be more easily integrated with other applications and systems.

For example, if you're building a weather application that needs to retrieve temperature data from a weather API, you might use filtering to retrieve only the data for a specific geographic location. This would allow you to integrate weather data with other parts of your application, such as a map or a location-based search feature.

Improved data quality ✅

Finally, filtering can improve the quality of data returned by web APIs. By selecting only the relevant subset of data, developers can avoid including irrelevant or inaccurate data in their applications.

For example, if you're building a financial application that needs to retrieve stock prices from an API, you might use filtering to retrieve only the prices for a specific stock symbol. This would ensure that the data returned by the API is accurate and relevant to the application.

Clone the GitHub repo 🐙

As always if you want to follow along with the article clone the Blog API repository so you can have a hands-on experience applying filtering. Make sure you get the code from the Pagination branch as this contains the latest code up to this point.

Update the QueryParameters Class ⬆️

In the last article, we created a class called QueryParameters that holds the pagination parameters such as PageSize and PageNumber, and now we will add a filtering parameter to be able to filter by an author so the class will end looking like this.

public class QueryParameters
{
    const int maxPageSize = 50;
    public int PageNumber { get; set; } = 1;
    private int pageSize = 10;
    public int PageSize
    {
        get { return pageSize; }
        set { pageSize = (value > maxPageSize) ? maxPageSize : value; }
    }

    //Filtering params
    public string Author { get; set; } = string.Empty;
}

Everything is the same we just added the Author property at the end of the file and initialized it to an empty string.

Update your IPostRepository and PostRepository classes 🏛️

Once again we see ourselves in the need to update our IPostRepository and PostRepository classes to pass them the QueryParameters as a parameter instead of the raw params. This is to improve the way we add more and more parameters avoiding having a method with a lot of parameters being passed.

public interface IPostRepository 
{
    Task<IEnumerable<Post>> GetPostAsync(QueryParameters parameters);
}
public async Task<IEnumerable<Post>> GetPostAsync(QueryParameters parameters)
{
}

Now with this change in place, you can keep adding more and more params to the QueryParameters class without having to mess with the signature of any of both the interface and the class.

Applying filtering in the repository class 🚿

As with Pagination you want to apply the filtering in the repository class when you are querying the database for all our resources. Filtering should be applied before pagination because you want to filter against all the database resources, not after the data set is paginated.

public async Task<IEnumerable<Post>> GetPostAsync(QueryParameters parameters)
{
    var allPosts = context.Posts.AsQueryable();

    //Filter by Author
    if(!string.IsNullOrEmpty(parameters.Author))
    {
        allPosts = allPosts.Where(x => x.Author == parameters.Author);
    }

    var pagedPosts = await allPosts
            .Skip((parameters.PageNumber - 1) * parameters.PageSize)
            .Take(parameters.PageSize)
            .ToListAsync();

    return pagedPosts;
}

Filtering is added above pagination but before you need to check if the Author parameter is empty or null, as if it’s empty it means the client is not asking for filtering, if not then you will retrieve only the posts where the author is the specified one. No call to the ToListAsync method is made on the query as you still need to paginate before actually executing the query to the database.

Test your API filtering by Author 🧪

Now take your web API to test the new filtering functionality, you should add some posts with the same author if you don’t have any to actually see the filtering in action. Now this easy is how you add filtering to your web API, there are actually other ways to implement filtering, I showed you a pretty simple and straightforward to do it but if you look into the internet you will find some more crazy and improved ways to do it.

Conclusion 🌇

Filtering is an essential and useful feature of Web APIS that allows developers to get subsets of data from a larger dataset based on some specific criteria. By implementing filtering in their web APIs. developers can reduce data transfer from the server to the client and we already know that this is always something that we want as this saves time, improves performance, consume fewer resources, and improves the user experience as he can demand to see only relevant data to him. But as with everything it is not a silver bullet and you should always consider if filtering is something that will actually improve your application and that it makes sense to have it or if it will just add more complexity and make no change.

Thanks for your support, now follow me on social media!

As always thanks for reading my articles and I hope you are getting a lot from reading them for your daily coding and work. Remember to follow me on my blog Unit Coding and my YouTube channel even though I have not uploaded any content in a while 😅 also now you can follow me on Twitter as Oscar Montenegro I’m still new to Twitter so I don’t have that many posts but I will be posting some content there so you know what I’m up to on my daily life as a developer.

Thanks for everything and I hope you have an amazing weekend, enjoy it, take some time to rest and be with your loved ones, and as always, keep coding!

Did you find this article valuable?

Support Unit Coding by becoming a sponsor. Any amount is appreciated!