I want to get the data and display it in sorted (child below parent).
Data items defined as follows: ID | Title | Parent id
What I am doing is taking all the elements first and then sorting.
Is there a better way to do this with linq?
protected void Page_Load(object sender, EventArgs e) { List<Category> list2 = new List<Category>(); ContModel modeltx = new ContModel(); var ret = modeltx.Categories.ToList(); GetCategoryList(0, 0, ret, list2); string str=""; foreach (Category cat in list2) { str=str+cat.Title+"\n"; TextBox1.Text = str; } } private void GetCategoryList(int iCurID, int iDepth, List<Category> li, List<Category> newList) { Category tmp; string strOffset = ""; foreach (Category cat in li) { if ((cat.ParentId) == iCurID) { for (int i = 1; i <= iDepth; i++) strOffset = strOffset + "-"; strOffset = strOffset + cat.Title; tmp = cat; tmp.Title = strOffset; newList.Add(tmp); strOffset = ""; GetCategoryList(cat.CategoryID, iDepth + 1, li, newList); } } }
Update:
What if the data size is huge and I want to use paging?
I can't Page ( .Skip(PageSize * PageIndex).Take(PageSize) ) before sorting ...
source share