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..
If you do not want to use Dynamic Linq the following see Marc Gravell’s answer below
stackoverflow:Dynamic Linq OrderBy