我想要检索数据并将其显示在已排序的子级(父级以下的子级)中。
数据项定义如下:ID |标题|家长ID如何使用Linq-to-entities检索分层数据?
我所做的是首先检索所有项目,然后排序。
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);
}
}
}
更新:
如何做,如果数据的规模是巨大的,我想使用分页?
我不能排序之前页(.Skip(PageSize * PageIndex).Take(PageSize)
)...
可能的重复http://stackoverflow.com/questions/202912/hierarchical-data-in-linq-options-and-performance – too 2011-01-06 20:18:56