ASP.NET MVC LINQ对2列进行排序,以便在WebGrid中使用

时间:2014-02-12 10:53:35

标签: sql-server asp.net-mvc

这是我的控制器中的示例代码段:

public ViewResult Index(int id)
{
    var viewModel = new IssueFileIndexViewModel();

    viewModel.IssueFile = db.IssueFiles.Include(i => i.Issue).Where(i => i.Id == id).ToList();

    return View(viewModel);
}

这是我在视图中的WebGrid:

@{
    var grid = new WebGrid(source: Model.IssueFile,
                    rowsPerPage: 15,
                    canPage: true,
                    canSort: true,
                    fieldNamePrefix: "gridItems_",
                    pageFieldName: "inside",
                    selectionFieldName: "selectedRow"
                );

    @grid.GetHtml(tableStyle: "hovered",

            columns: grid.Columns(

                    grid.Column(
                        columnName: "Volume",
                        header: "Volume"
                    ),

                    grid.Column(
                        columnName: "Number",
                        header: "Number"
                    ),
            )
        )   
}

以下是我想按卷和数字排序的示例表:

enter image description here

如何按体积升序和数字降序对其进行排序。像这样:

enter image description here

我希望自己清楚明白。 :(

- 编辑 -

这是我的疑问:

SELECT Id, Volume, Number from IssueFiles WHERE Id = @0id
ORDER BY
 CASE WHEN ISNUMERIC(Volume) = 1
 THEN
    Volume
 ELSE 
    CASE 
        WHEN ISNUMERIC(SUBSTRING(Volume, 0, 10)) = 1 
        THEN 
            CAST(SUBSTRING(Volume, 0, 10) AS INT)
        WHEN ISNUMERIC(SUBSTRING(Volume, 0, 9)) = 1 
        THEN 
            CAST(SUBSTRING(Volume, 0, 9) AS INT)
        WHEN ISNUMERIC(SUBSTRING(Volume, 0, 8)) = 1 
        THEN 
            CAST(SUBSTRING(Volume, 0, 8) AS INT)
        WHEN ISNUMERIC(SUBSTRING(Volume, 0, 7)) = 1 
        THEN 
            CAST(SUBSTRING(Volume, 0, 7) AS INT) 
        WHEN ISNUMERIC(SUBSTRING(Volume, 0, 6)) = 1 
        THEN 
            CAST(SUBSTRING(Volume, 0, 6) AS INT)
        WHEN ISNUMERIC(SUBSTRING(Volume, 0, 5)) = 1 
        THEN 
            CAST(SUBSTRING(Volume, 0, 5) AS INT)
        WHEN ISNUMERIC(SUBSTRING(Volume, 0, 4)) = 1 
        THEN 
            CAST(SUBSTRING(Volume, 0, 4) AS INT)
        WHEN ISNUMERIC(SUBSTRING(Volume, 0, 3)) = 1 
        THEN 
            CAST(SUBSTRING(Volume, 0, 3) AS INT)
        WHEN ISNUMERIC(SUBSTRING(Volume, 0, 2)) = 1 
        THEN 
            CAST(SUBSTRING(Volume, 0, 2) AS INT)
        WHEN ISNUMERIC(SUBSTRING(Volume, 0, 1)) = 1 
        THEN 
            CAST(SUBSTRING(Volume, 0, 1) AS INT)
        WHEN ISNUMERIC(SUBSTRING(Volume, 0, 0)) = 1 
        THEN 
            CAST(SUBSTRING(Volume, 0, 0) AS INT)
    END
 END
ASC, Number ASC;

我现在的问题是如何将其转换为LINQ查询。请帮忙!谢谢!

1 个答案:

答案 0 :(得分:0)

尝试在检索中添加订单:

viewModel.IssueFile = db.IssueFiles.Include(i => i.Issue).Where(i => i.IssueID == _IssueID).Order(i => i.Volume).ToList()