Saturday, 18 May 2013
MVC 4 - how to fix error Cannot implicitly convert type 'System.Linq.IOrderedQueryable' to 'System.Data.Entity.DbSet'.
Watch on youtube
Error message
Cannot implicitly convert type 'System.Linq.IOrderedQueryable<MVCTestRizne.Models.Dpt>' to 'System.Data.Entity.DbSet<MVCTestRizne.Models.Dpt>'.
An explicit conversion exists (are you missing a cast?) C:\LocalDoc\VS2012Projects\Test\MVCTestRizne\MVCTestRizne\Controllers\DptController.cs
Replace
public ActionResult Index(string SortBy)
{
ViewBag.NameSortParam = String.IsNullOrEmpty(SortBy) ? "Name Desc" : "";
// return View(db.Dpts.ToList());
var departments = db.Dpts;
switch (SortBy)
{
case "Name Desc":
departments = departments.OrderByDescending(s => s.DepartmentName);
break;
default:
departments = departments.OrderBy(d => d.DepartmentName);
break;
}
return View(departments.ToList());
}
with
public ActionResult Index(string SortBy)
{
ViewBag.NameSortParam = String.IsNullOrEmpty(SortBy) ? "Name Desc" : "";
// return View(db.Dpts.ToList());
var departments = db.Dpts.Include(u => u.Users);
switch (SortBy)
{
case "Name Desc":
departments = departments.OrderByDescending(s => s.DepartmentName);
break;
default:
departments = departments.OrderBy(d => d.DepartmentName);
break;
}
return View(departments.ToList());
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment