011. Adding Search to an ASP. NET Core MVC app, 011. addingmvc
Adding Search to an ASP. NET Core MVC app
Add a search function to a program
Author of 7-minute reading duration
Content
1. Adding Search by genre
Add search by music genre
2. Adding search by genre to the Index view
Add music genre search in Index View
By Rick Anderson
In this section you add search capability to the Index action method that lets you search moviesGenreOrName.
In this section, we will add the search function to the Index method to search for music genre or music name.
Update the Index method with the following code:
Use the following code to update the Index:
1 public async Task <IActionResult> Index (string searchString) 2 3 {4 5 var movies = from m in _ context. Movie 6 7 select m; 8 9 10 11 if (! String. isNullOrEmpty (searchString) 12 13 {14 15 movies = movies. where (s => s. title. contains (searchString); 16 17} 18 19 20 21 return View (await movies. toListAsync (); 22 23}C # code
The first line of the Index action method creates a LINQ query to select the movies:
The first line of the Index method writes a LINQ query expression to query movie data:
1 var movies = from m in _ context. Movie2 3 select m;C # code
The query isOnlyDefined at this point, it hasNotBeen run against the database.
This query expression is defined only at the moment. It is inert and will not be executed in database requests.
If the searchString parameter contains a string, the movies query is modified to filter on the value of the search string:
The searchString parameter contains a string. The movie query filters the query data based on the search string:
1 if (! String. IsNullOrEmpty (id) 2 3 {4 5 movies = movies. Where (s => s. Title. Contains (id); 6 7}C # code
The s => s. Title. Contains () code above is a Lambda Expression.
The above s => s. Title. Contains () is a Lambda expression.
Lambdas are used in method-based LINQqueries as arguments to standard query operator methods such as the Where method or Contains (used in the code above ).
Lambda expressions are used for standard operation functions based on functions as parameters, such as the Where method or the preceding Contains method.
LINQ queries are not executed when they are defined or when they are modified by calling a method such as Where, Contains or OrderBy.
The LINQ Query expressions are not executed when they are defined. They are not executed when they are modified by Where, Contains or OrderBy.
Rather, query execution is deferred.
The execution of the query is delayed, and it is executed in inertia.
That means that the evaluation of an expression is delayed until its realized value is actually iterated over or the ToListAsyncmethod is called.
That is to say, the evaluation of the expression will be delayed until the real traversal or the ToListAsync method is called to start the calculation of the expression value.
For more information about deferred query execution, see Query Execution.
View Query Execution to obtain