Need help understanding LINQ in MVC?

I'm new to webforms MVC, and for me it was a really cool learning curve. Below is the following function in the tutorial:

public ActionResult Index(string id) { string searchString = id; var movies = from m in db.Movies select m; if (!String.IsNullOrEmpty(searchString)) { movies = movies.Where(s => s.Title.Contains(searchString)); } return View(movies); } 

That's what I think I know this is happening. This method is the result of an action (without parameters) that returns a view. Parameters are added to tell the application to search for the string "id".

I find the lambda statement a little easier to understand. if checks if searchString is null if it does not return a movie that matches the description in searchString .

In the method, however, searchString is set to the id value in the parameter. This is where I start to be mistaken, immediately after defining searchString the LINQ statement is placed inside the movies variable. In this statement, what is the purpose of m ? Why is this not defined or is it not? Same thing with s in lambda.

+8
c # linq asp.net-mvc
source share
5 answers

Both m and s are implicitly entered. Since you select m from movies , you do not need to tell LINQ that m . This may mean type, considering db.Movies is a compilation. So if db.Movies was IEnumerable<Movie> (for example), then m would be Movie .

You have nothing to interfere with indicating the type if you really want to, so you can enter:

  var movies = from Movie m in db.Movies select m; 

But you rarely have to.

Note that you also implicitly print movies , this is the same concept. While the compiler can unambiguously determine what type should be.

+5
source share

The variables m and s are the variables for each instance of Movie in the db.Movies collection "(I assume that is called a class).

Conceptually, they are similar to using sql alias m in the following sql:

 select m.* from Movies m 

The next time you use the where clause, you conceptually end:

 select * from ( select m.* from Movies m ) s where s.Title like '%' + searchString + '%' 

NOTE: this is not how sql really ends when it works with the database, just a view to help you understand.

If you look at the value of movies , you will see that it is IQueryable or the like. This means that it has not yet been completed - you have not returned anything. Thus, adding a where clause will be great - it is simply added to the query, which will be launched later.

+4
source share

var movies = from m in db.Movies select m roughly translates to "Take all the elements in db.Movies and name them temporarily m, and then return them to IEnumerable." Indeed, you will see that movies is of type IEnumerable<Movie> .

The same applies to movies = movies.Where(s => s.Title.Contains(searchString)); : For all elements in movies, temporarily name them s and return those whose Title contains your searchString as IEnumerable .

Hope this has become a little clearer.

+1
source share

Good - I will try to explain what is happening:

from:

 var movies = from m in db.Movies select m; 

You describe how to handle the "db.Movies" collection (whatever that is ...)

In descriptive language:

1) in dbo.Movies We are going to check / in dbo.Movies over anything in db.Movies.

2) from m As we go through them 1-on-1, we will store every thing that we encounter in a variable called "m" for later use in the expression.

3) select m Okay - we want this query / expression to really return something - to really give some results - so, let's 1-on-1 just return the 'm' we announced earlier

+1
source share

The variables m and s use the var keywords, which means that you do not need to explicitly specify the type of the variable. The compiler calculated this for you. This is basically an IEnumerable type variable.

The var keyword is usually useful in the following if you cannot explicitly determine the return type of movies , as shown below -

 var movies = from m in db.Movies select new { m.Attr1, m.Attr2 } ; 

Since the sentence in new is another "anonymous" object not specified anywhere, you need the var keyword.

0
source share

All Articles