Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Iam a beginner in MVC and now iam trying to study mvc using asp.net mvc tutorials.From there i had find a code to generate a dropdown list from dbcontext(entity framework).
C#
public ActionResult Index(string movieGenre, string searchString)
{
    var GenreLst = new List<string>();

    var GenreQry = from d in db.Movies
                   orderby d.Genre
                   select d.Genre;

    GenreLst.AddRange(GenreQry.Distinct());
    ViewBag.movieGenre = new SelectList(GenreLst);

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

    if (!String.IsNullOrEmpty(searchString))
    {
        movies = movies.Where(s => s.Title.Contains(searchString));
    }

    if (!string.IsNullOrEmpty(movieGenre))
    {
        movies = movies.Where(x => x.Genre == movieGenre);
    }

    return View(movies);
}


i had changed this code and re-witten as

C#
public ActionResult Index(string movieGenre, string searchString)
        {
           // List<string> GenreList = new List<string>();
            var GenreQry = from d in db.Movies orderby d.Genre select d.Genre;
            GenreQry = GenreQry.Distinct();
            //GenreList.AddRange(GenreQry.Distinct());
            ViewBag.movieGenre = new SelectList(GenreQry);

            var movies = from m in db.Movies select m;
            if (!string.IsNullOrEmpty(searchString))
            {
                movies = movies.Where(m => m.Title.Contains(searchString));
            }

            if(!string.IsNullOrEmpty(movieGenre))
            {
                movies = movies.Where(x => x.Genre == movieGenre);
            }

            //return View(db.Movies.ToList());
            return View(movies);
        }


My code also works fine.Then what is the need of GenreLst that is of list type. I just want to know the difference because now am in a learning stage
Posted

1 solution

A Linq query (or a Linq method, such as Distinct in your code) returns an IEnumerable collection, rather than a List<T> - provided that your SelectList constructor takes an IEnumerable rather than a specific collection such as List (which implements IEnumerable) you can pass it any collection.

A List<T> is a specific collection which implements IEnumerable - it's possible that the original example of the SelectList constructor only accepted a List<T> so it was necessary to create one specifically to pass through.

However, you can always generate one:
C#
GenreList = GenreQry.ToList();
Will do it rather simpler.
 
Share this answer
 
Comments
Namith Krishnan E 5-Jan-15 2:27am    
I would like to know what actually is IEnumerable.I have searched about it and i didn't understand anything...Please help
OriginalGriff 5-Jan-15 5:21am    
Ok...do you know what an Interface is? And how it differs from a class?

If not, start by reading up on them, and then try summarising them in your own words - copying from somewhere else won't help as there is no point in continuing to specific interfaces and such like unless you understand the basics!
When you are ready, explain an Interface to me! :laugh:
Namith Krishnan E 5-Jan-15 5:45am    
i know interface
OriginalGriff 5-Jan-15 5:58am    
Good. So tell me what it is.
Namith Krishnan E 6-Jan-15 23:50pm    
By using interface we are enforcing the derived class to use the methods in the interface by overriding it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900