这有什么区别? [英] What is the difference between this?
问题描述
我是MVC的初学者,现在我尝试使用asp.net mvc教程学习mvc。从那里我找到了一个代码来从dbcontext(实体框架)生成一个下拉列表。
< pre lang =c#> public ActionResult Index( string movieGenre, string searchString)
{
var GenreLst = new 列表与LT;串GT;();
var GenreQry = 来自 d in db.Movies
orderby d.Genre
选择 d.Genre;
GenreLst.AddRange(GenreQry.Distinct());
ViewBag.movieGenre = new SelectList(GenreLst);
var movies = 来自 m in db.Movies
选择 m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s = < span class =code-keyword>> s.Title.Contains(searchString));
}
if (!string.IsNullOrEmpty(movieGenre))
{
movies = movies。 Where(x = > x.Genre == movieGenre);
}
return 查看(电影);
}
i已更改此代码并重新确认为
public ActionResult Index( string movieGenre, string searchString)
{
// List<串GT; GenreList = new List< string>();
var GenreQry = 来自 d db.Movies orderby d.Genre select d.Genre;
GenreQry = GenreQry.Distinct();
// GenreList.AddRange(GenreQry.Distinct());
ViewBag .movieGenre = new SelectList(GenreQry);
var movies = 来自 m in db.Movies 选择 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 查看(电影);
}
我的代码也可以正常工作。那么GenreLst的需求是列表类型。我只是想知道区别,因为现在处于学习阶段
Linq查询(或Linq方法,例如代码中的Distinct)返回一个IEnumerable集合,而不是List< T> - 如果您的SelectList构造函数采用IEnumerable而不是特定集合(例如List(实现IEnumerable)),您可以将它传递给任何集合。
A List< T>是一个实现IEnumerable的特定集合--SelectList构造函数的原始示例可能只接受List< T>。因此有必要创建一个专门用于通过。
但是,你总是可以生成一个:
GenreList = GenreQry.ToList();这样做会更简单。
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).
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
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
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:
GenreList = GenreQry.ToList();Will do it rather simpler.
这篇关于这有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!