2

我想在我的查询中使用多个 order by,并且我正在使用 LINQ。我是 LINQ 的新手,我已经尝试了 stackoverflow 上给出的示例。但我不知道为什么这些对我不起作用,我确信我在某些地方错了。下面是我的情况。我有一个使用 LINQ 创建的项目。我几乎没有设置列顺序的任务。实际上我的任务是有一个列创建日期,现在它的排序。现在我想使用 Createddate 以及 sortOrder 列进行排序。下面是用于它的代码:页面加载方法中的代码

ViewState["SortDirection"] = "desc";
ViewState["SortColumn"] = "createddate";
BindAllTopics(ViewState["SortDirection"].ToString(), ViewState["SortColumn"].ToString());

我的BindAllTopic方法如下:

 protected void BindAllTopics(string SortType, string SortColumn)
{
    var LstTopics = (from topic in Dbobj.T_topic select topic).ToList();
    if (LstTopics.Count() > 0)
    {
        if (SortType == "ASC")
        {
            LstTopics = LstTopics.OrderBy(q => q.Name).ToList();
        }
        else
        {
            LstTopics = LstTopics.OrderByDescending(q => q.Name).ThenBy(q => q.SortOrder).ToList();
        }
        GrdTopics.DataSource = LstTopics.ToList();
        GrdTopics.PageSize = 25;
        GrdTopics.DataBind();

    }
    else
    {
        GrdTopics.DataSource = null;
        GrdTopics.DataBind();
        lblMsg.DisplayMessage(StatusMessages.InfoMessage, "No Topics Found.");
    }
}

我想首先按 int 类型的 sortorder 排序,然后按 Createddate 排序。

请帮我..

4

3 回答 3

8

您可以在跟踪中再添加一个 ThenBy()

LstTopics.OrderByDescending(q => q.Name).ThenBy(q => q.SortOrder).ThenBy(m=>m.date)
于 2012-07-19T13:04:55.717 回答
3

如果您不想使用 Dynamic Linq,请参阅以下 Marc Gravell 的回答

stackoverflow:Dynamic Linq OrderBy

于 2012-07-19T13:47:08.020 回答
1

更正后,您可以通过 Dynamic Linq 实现此目的

LstTopics = LstTopics.OrderBy(SortColumn + " " +  SortType);)
于 2012-07-19T13:05:44.200 回答