Before starting learning C Sharp to implement predicate builder for filtering data in Net Core, you should first watch the video mamank in the previous video regarding the basics of how to make an API using .Net Core because the current project is a continuation of the previous project regarding implementation of how to create an API in NET CORE.
Open the project previously, namely the project with the project name implementation netcore in the project now we will use a library which in the past mank made a library for project needs and it is also useful for this project on dependencies right click => add project reference then browse the library with the name nusantaratech.dll for whom those who need or are interested in the library that made, please leave a message in the comments column.
To see the libraries that have been included, you can see them in the assemblies section. We can see that nusantaratech.dll has been included. The difference with the previous project is that in the previous project, we immediately defined the IUnitOFWork interface in the Developer Controller. In this project, we will move to the BaseController section, which The DeveloperController is set to inherit to the BaseController as well as other controllers that will be made . Next, we will add a script to the base controller in the form of json will be handled by the library that has been created.
Now we go to the next step to implement the predicate builder in the C# programming language using Net Core. First define the function in the IDeveloperRepository interface we will define the function used for filtering data using the predicate builder, for example, we create a function called GetDeveloperByPB(), add a parameter for request data to be filtered, for how to add a class, please watch the previous video about how to make an API using .NET CORE and implement how to add classes to this project after defining the function on the next IDeveloperRepository interface.. implement this function on DeveloperRepository because the function defined is like Task<IEnumerable<Developer>> then add async to the implementation of the function first, we declare a variable in the form of list data to accommodate return data in the form of IEnumerable<Developer> and variable declaration to hold the countofdata data now we define a variable to hold the two data then we add try{ }catch(exception){} so we can use the predicate builder add a package or nuget package to DataAccess.EFCore the name of the package a is LinqKit.Core for data filtering needs using the predicate builder in the try block.. first we define the predicate builder variable to add the data to be filtered to the predicate builder first check whether the data in the request parameter is not empty or null if the request data is not null, then add to the predicate builder variable that has been defined earlier this is the same as SELECT * FROM Developer Where Name = @name, @name is the variable that is sent, for example if we use a stored procedure then we will try to add to filter data based on Experience Developer in the case this will not use AND but using OR this means the same as SELECT * FROM Developer Where Name = @name OR Experience = @exp, if we compare to the sql server query then make a linq query to display Developer data with our predicate builder parameter set to default null then we fill in the qry variable with the query linq for k filtering data using the predicate builder, the meaning of the linq query is SELECT * FROM Developer Where Name = @name OR Experience = @exp if on the sql server then fill in the data that has been previously defined with the data list from the results of the linq query the difference lies in when not using async then use the ToList() keyword but when using async then use ToListAsync() then check the data, is the data null or not if it is not null then fill in the countData variable with data.Count() because the return function is IEnumerable<Developer> and CountOfData so return (data, countData) is simple, isn't it? that's a simple way to use the Predicate builder to filter data first define the predicate builder variable secondly fill the variable with data to be filtered third create a linq query to display data based on the predicate builder and finally we set the return of the function with listdata and amount of data after defining the function in IDeveloperRepository and implementing it in DeveloperRepository then we will use or call the function from the controller (DeveloperController) for example in DeveloperController we will create an API to display developer data based on filter data with predicate builder so c sharp programming language tutorial for how to use predicate builder for filtering data on .net core, if you need a complete source code for learning this time, please leave a message in the comments column, see you again in the next episode, thank you for visiting the Mamank Koding channel
video txt