简体   繁体   中英

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.

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);
    }

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? 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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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