如何使用linq对列进行排序

时间:2015-10-15 02:41:31

标签: c# asp.net-mvc entity-framework linq

这是我在哪里尝试排序

sumatoriafinal = input(">")
varianza = int(sumatoriafinal) / datos

我用linq编写了我的查询,但是我想对orderby做一个排序我在一个表上用实体框架做了这个成功但不知道我如何在我的连接上的这些表上执行此操作

 <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>

所以按照我的顺序,我希望传入strSortby,就像我在这部分所做的那样,但是我可能做得不对。这是我在其他控制器上写的开关,它成功完成了排序。

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

1 个答案:

答案 0 :(得分:0)

这是我做过的代码谢谢你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());