linq在c中只得到一个级别的递归列表
本文关键字:一个 列表 递归 linq | 更新日期: 2023-09-27 17:59:13
希望能够提取递归列表的前两个级别。
表
InventoryTypeId int
nvarchar(50)型
ParentId int
public IEnumerable<inventoryTypeModel> getInventoryTypes()
{
var _type = from d in dbgpsContext.InventoryType
where d.ParentId==null
select new inventoryTypeModel
{
typeid = d.InventoryTypeId ,
type = d.Type,
subtypes=???//no sure what goes here
};
return _type.ToList();
}
像这样的东西应该有魔力:
public IEnumerable<inventoryTypeModel> getInventoryTypes()
{
var _type = from d in dbgpsContext.InventoryType
where d.ParentId==null
select new inventoryTypeModel
{
typeid = d.InventoryTypeId ,
type = d.Type,
subtypes= from s in d
where ParentId == d.Id
select s.Type
};
return _type.ToList();
}
希望这能有所帮助,因为我不知道你们的模型之间到底有什么关系。
这里是LINQ方法链版本
public IEnumerable<inventoryTypeModel> getInventoryTypes()
{
var _type = dbgpsContext.InventoryType.Where(d=> d.ParentId == null).Select(b => new
{
Typeid = b.InventoryTypeId ,
Type = b.Type,
Subtypes = dbgpsContext.InventoryType.Where(v=> v.ParentId == b.Id).Select(c => c.Type)
}).ToList();
return _type;
}