简体   繁体   English

在 MVC5 中使用 LINQ 和 C#

[英]Using LINQ with C# in MVC5

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.我想要做的是按姓氏在数据库上使用 Sort lambda。

EmployeesController:员工控制器:

public ActionResult Index()
    {
        return View(m.EmployeeGetAll());
    }

manager.cs:经理.cs:

public IEnumerable<EmployeeBase> EmployeeGetAll()
    {
        //use automapper to map objects, source to target
        return mapper.Map<IEnumerable<Employee>, IEnumerable<EmployeeBase>>(ds.Employees);
    }

Database value:数据库值:

    [Required]
    [StringLength(20)]
    public string LastName { get; set; }

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?使用上述方法可以显示数据库中所有员工的列表,但是在我的控制器或方法中我在哪里使用 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?如果你在 manager.cs 中创建一个 method() 并在其中使用 lambdas 来返回一个对象,我基本上被困住了,你如何为索引视图调用它? 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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM