Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help understanding LINQ in MVC?

I am quite new to MVC from webforms and it has been a really, really steep learning curve for me. Here is the function in the tutorial that I am following:

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);
}

Here is what I think I know that is happening. The method being an Action result(without parameters) re returning a view. The parameters are added to tell the application to look for a "id" string.

I find the lambda statement a little easier to understand. The if is checking to see if the searchString is null if not it returns the movie that matches the description in searchString.

In the method however, searchString is given the value of id in the parameter. Here is where I start getting lost, right after searchString is defined, a LINQ statement is placed inside the variable movies. In that statement, what is the purpose of m? Why is it not defined or is it? The same with the s in the lambda.

like image 987
Skullomania Avatar asked Apr 23 '15 13:04

Skullomania


2 Answers

Both m and s are implicitly typed. Since you are selecting m from movies, you don't need to tell LINQ what m is. It can imply the type by looking at what db.Movies is a collection of. So if db.Movies was IEnumerable<Movie> (for example) then m would be a Movie.

There is nothing to stop you from specifying the type if you really want to, so you could type:

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

But you rarely need to.

Note you are also implicitly typing movies, it's the same concept. So long as the compiler can unambiguously figure out what the type should be.

like image 62
Matt Burland Avatar answered Oct 16 '22 08:10

Matt Burland


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

Conceptually these are similar to using and sql alias m in the following sql:

select m.* from Movies m

When you later apply the where clause you're conceptually ending up with:

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

NOTE that this isn't how the sql actually ends up when it runs against the database, just a representation to aid understanding.

If you look at the value of movies then you'll see that it is an IQueryable or similar. This means that it hasn't actually executed yet - you've not returned anything. So adding the where clause later is fine - it just gets added to the query that'll get run later.

like image 44
Jon Egerton Avatar answered Oct 16 '22 08:10

Jon Egerton