【发布时间】:2011-01-06 15:17:21
【问题描述】:
我想检索数据并按排序方式显示(子项低于其父项)。
数据项定义如下: ID |标题 |父 ID
我所做的是首先检索所有项目,然后进行排序。
有没有更好的方法可以用 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);
}
}
}
更新:
如果数据量很大,我想使用分页怎么办?
在排序之前我不能 Page (.Skip(PageSize * PageIndex).Take(PageSize)) ...
【问题讨论】:
标签: c# asp.net linq-to-entities