Adding Filter Feature to the API

In this lesson, we're going to add filter feature to our API

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:07] We can now sort our results in an ascending or descending way. Another important feature can be filtering the search results.

    [00:08 - 00:21] That can be on the basis of an instructor, a category, price range and so on. This will again use a criteria where we are using the "where" statement to filter the search results.

    [00:22 - 00:28] Let's filter the results based on category in this lecture. We will add more options in the future.

    [00:29 - 00:38] So what we'll do is inside "courses controller" and get "courses method". Let's pass another parameter to it which will be "category" and type int.

    [00:39 - 01:02] So I will use int and call it "category id" and we'll pass it to "courses with category specification category id". It will show us an error so we can go here and add it here as well.

    [01:03 - 01:16] And now the error is gone. And one more thing, it's not always going to be permanent so we can add a question mark which will make it optional.

    [01:17 - 01:24] And we see an error because it's optional here but not here. So let's make it optional here as well.

    [01:25 - 01:41] We have to use our criteria so what we'll do is extend the base. If there is a value we can use an expression to check the category id with the "courses category id".

    [01:42 - 01:48] So how will we do it? Since our criteria takes an expression so we will use an expression here as well.

    [01:49 - 02:10] And first of all we will check if the integer has any value. So what we'll do is category id has value or we'll check if the x.category id is equal to the category id that we have passed.

    [02:11 - 02:29] First of all it checks if there is a value and it should be a bang operator. So first of all we are checking it's not empty or this category id is equal to the category id that is there in our course and that's simply it.

    [02:30 - 02:41] What we can do is again go to the postman, make sure the server is running and run filter by category request. Press send.

    [02:42 - 03:00] We see all the courses here but if we change the id to 2 or any other id let's say 4. We won't see any courses because we haven't added any other category to the courses.

    [03:01 - 03:07] Well our filtering works as well. Let's work on patching the result in the next lecture.