簡體   English   中英

在 MVC5 中使用 LINQ 和 C#

[英]Using LINQ with C# in MVC5

我被困在一個我似乎無法解決的問題上。 我想要做的是按姓氏在數據庫上使用 Sort lambda。

員工控制器:

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

經理.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() 並在其中使用 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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM