我有一个数据库表,它表示具有多级层次结构的帐户。每行都有一个代表当前账户的“AccountKey”,可能还有一个代表父代的“AccountKey”的“ParentKey”。在LINQ中创建层次结构
我的模型类是“AccountInfo”,其中包含关于账户本身的一些信息,和子帐户列表。
什么是这款平板数据库结构改造成一个层次结构最简单的方法?它可以直接在LINQ中完成,还是需要在事实之后循环并手动构建它?
型号
public class AccountInfo
{
public int AccountKey { get; set; }
public int? ParentKey { get; set; }
public string AccountName { get; set; }
public List<AccountInfo> Children { get; set; }
}
LINQ
var accounts =
from a in context.Accounts
select new AccountInfo
{
AccountKey = a.AccountKey,
AccountName = a.AccountName,
ParentKey = a.ParentKey
};
所以,你有什么是SQL邻接表模型(层次通过的ParentId的方式在一个表中保持)。你想将这个表转换为不同的结构,还是保留现有的结构(带有parentId的单个表)? –