在MVC5中将LINQ与C#结合使用 [英] Using LINQ with C# in MVC5
问题描述
我陷入了似乎无法解决的问题.我想做的是对数据库使用按其姓氏排序的lambda.
I am stuck on a problem I can't seem to solve. What I want to do is use a Sort lambda on a database by their last names.
EmployeesController:
EmployeesController:
public ActionResult Index()
{
return View(m.EmployeeGetAll());
}
manager.cs:
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; }
使用上述方法可以很好地显示数据库中所有雇员的列表,但是我在控制器或方法中的什么地方使用lamda?如果您在manager.cs中创建method()并在其中使用lambda来返回对象,那么我基本上会陷入困境,对于索引视图,您如何称呼它呢?正如我想要的那样,页面加载时将对姓氏进行排序.感谢您的帮助.
with the above methods it is fine to show a list of all the employees in the db, but where in my controller or methods do i use the lamda? I am basically stuck on if you make a method() in the manager.cs and use lambdas within it to return an object, how do you call this for the index view? As i want when the page loads for the last names to be sorted. Any help is appreciated.
推荐答案
我认为您应该在数据库级别进行排序,如下所示:
I think you should sort in database level, like this:
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);
}
这篇关于在MVC5中将LINQ与C#结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!