如何使列排序与linq

本文关键字:linq 排序 何使列 | 更新日期: 2023-09-27 18:17:42

这是我的视图,我试图排序

 <tr>
    <th>
        @Html.ActionLink("WardName", "CompletedReq", new { strSortBy = "WardName" })
       @*@ @Html.DisplayNameFor(model => model.WARD_NAME)*@
    </th>
    <th>
        @Html.ActionLink("WardLocation", "CompletedReq", new { strSortBy = "WardLocation" })
       @*@ @Html.DisplayNameFor(model => model.WARD_LOCATION)*@
    </th>
    <th>
        @Html.ActionLink("ItemID", "CompletedReq", new { strSortBy = "ItemID" })
        @*@Html.DisplayNameFor(model => model.ITEM_ID)*@
    </th>
    <th>
        @Html.ActionLink("ItemType", "CompletedReq", new { strSortBy = "ItemType" })
        @*@Html.DisplayNameFor(model => model.ITEM_TYPE)*@
    </th>
    <th>
        @Html.ActionLink("ItemName", "CompletedReq", new { strSortBy = "ItemName" })
        @*@Html.DisplayNameFor(model => model.ITEM_NAME)*@
        </th>
    </tr>

我用linq写了我的查询,但我想对顺序进行排序,我在一个表上成功地使用实体框架进行了排序,但不确定如何在我的连接上对这些表进行排序

     var query =
                       from wr in db.Ward_Req
                       join w in db.Wards
                       on wr.WARD_ID equals w.WARD_ID
                       join rl in db.Req_Line
                       on wr.REQ_ID equals rl.REQ_ID
                       join m in db.Materials
                       on rl.ITEM_ID equals m.ITEM_ID
                       where wr.STATUS == "C"
                       orderby w.WARD_NAME descending
                       select new Reports
                       {
                           WARD_NAME = w.WARD_NAME,
                           WARD_LOCATION = w.WARD_LOCATION,
                           ITEM_ID = m.ITEM_ID,
                           ITEM_TYPE = m.ITEM_TYPE,
                           ITEM_NAME = m.ITEM_NAME
                       };
            return View(query.ToList());

所以在我的顺序中,我想传递strSortby,就像我在这部分做的那样,但我可能做得不对。这是我在另一个控制器上写的开关,它成功地完成了排序。

     public ActionResult Index(string strSortBy)
    {            
        var staffs = db.Staffs.Include(s => s.Staff2);
        switch (strSortBy)
        {
            case "fname":
                staffs = staffs.OrderBy(s => s.FNAME);
                break;
            case "lname":
                staffs = staffs.OrderBy(s => s.LNAME);
                break;
            case "phone":
                staffs = staffs.OrderBy(s => s.TELEPHONE);
                break;
            case "gender":
                staffs = staffs.OrderBy(s => s.GENDER);
                break;
            case "position":
                staffs = staffs.OrderBy(s => s.POSITION);
                break;
            case "status":
                staffs = staffs.OrderBy(s => s.STATUS);
                break;
            case "username":
                staffs = staffs.OrderBy(s => s.USERNAME);
                break;
            default:
                break;
        }
        return View(staffs.ToList());
    }

如何使列排序与linq

这是我写的代码谢谢你Stephen Muecke,它工作得很完美,我只是在考虑它,你非常感激,尽管

            var query =
                       from wr in db.Ward_Req
                       join w in db.Wards
                       on wr.WARD_ID equals w.WARD_ID
                       join rl in db.Req_Line
                       on wr.REQ_ID equals rl.REQ_ID
                       join m in db.Materials
                       on rl.ITEM_ID equals m.ITEM_ID
                       where wr.STATUS == "C"
                       orderby w.WARD_NAME descending
                       select new Reports
                       {
                           WARD_NAME = w.WARD_NAME,
                           WARD_LOCATION = w.WARD_LOCATION,
                           ITEM_ID = m.ITEM_ID,
                           ITEM_TYPE = m.ITEM_TYPE,
                           ITEM_NAME = m.ITEM_NAME
                       };
            switch (strSortBy)
            {
                case "WardName":
                    query = query.OrderBy(s => s.WARD_NAME);
                    break;
                case "WardLocation":
                    query = query.OrderBy(s => s.WARD_LOCATION);
                    break;
                case "ItemID":
                    query = query.OrderBy(s => s.ITEM_ID);
                    break;
                case "ItemType":
                    query = query.OrderBy(s => s.ITEM_TYPE);
                    break;
                case "ItemName":
                    query = query.OrderBy(s => s.ITEM_NAME);
                    break;
                default:
                    break;
            }
            return View(query.ToList());