2

I want to use multiple order by in my query, and i am using LINQ.i am new to LINQ, i have tried the examples given on stackoverflow. but i dont know why these are not working for me , i am sure i am wrong some where. below is my situation. I got a project in which created using LINQ. I have little task to set order of column. Actually what is my task there is a column created date by which its ordering now. Now i want to use Createddate as well a sortOrder column for ordering.Below is code used for it: Code in page load method

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

And my BindAllTopic Method is as bellow:

 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.");
    }
}

I want to sort it firstly by sortorder which is of int type and then by Createddate.

Please help me..

Ram Singh
  • 5,979
  • 29
  • 93
  • 156

3 Answers3

8

you can add one more ThenBy() in trail

LstTopics.OrderByDescending(q => q.Name).ThenBy(q => q.SortOrder).ThenBy(m=>m.date)
indiPy
  • 7,115
  • 3
  • 25
  • 39
3

If you do not want to use Dynamic Linq the following see Marc Gravell's answer below

stackoverflow:Dynamic Linq OrderBy

Community
  • 1
  • 1
AlanT
  • 3,249
  • 16
  • 27
1

After correction you can achieve this by Dynamic Linq

LstTopics = LstTopics.OrderBy(SortColumn + " " +  SortType);)
HatSoft
  • 10,683
  • 3
  • 25
  • 43