在MVC5中使用LINQ和c#
本文关键字:LINQ MVC5 | 更新日期: 2023-09-27 18:08:38
我被一个我似乎无法解决的问题卡住了。我要做的是在数据库中按姓氏排序。
EmployeesController:
public ActionResult Index()
{
return View(m.EmployeeGetAll());
}
manager.cs:
public IEnumerable<EmployeeBase> EmployeeGetAll()
{
//use automapper to map objects, source to target
return mapper.Map<IEnumerable<Employee>, IEnumerable<EmployeeBase>>(ds.Employees);
}
数据库值:
[Required]
[StringLength(20)]
public string LastName { get; set; }
使用上述方法可以显示数据库中所有员工的列表,但是在我的控制器或方法中我使用lambda吗?如果你在manager.cs中创建一个方法()并在其中使用lambdas返回一个对象,你如何调用索引视图?因为我想当页面加载的姓氏被排序。
我认为你应该在数据库级排序,像这样:
public IEnumerable<EmployeeBase> EmployeeGetAll()
{
var all=ds.Employees.OrderBy(e=>e.LastName);
//use automapper to map objects, source to target
return mapper.Map<IEnumerable<Employee>, IEnumerable<EmployeeBase>>(all);
}