私はデータを取得し、それをソート済み(親の下の子)に表示したいと思います。
データ項目は次のように定義されています。タイトル|親IDLinq-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