【发布时间】:2016-06-11 04:15:41
【问题描述】:
我正在尝试显示数据库中的文章,但我为类别创建了递归表。所以问题是当父类别选择时,我无法从儿童类别中检索文章。
public class Categories
{
public int id { get; set; }
public string Category { get; set; }
public int? parentId { get; set; }
public IList<Categories> ChildMenu { get; set; }
}
和文章类
public class Article
{
public int id { get; set; }
public string Name{ get; set; }
public int CategoryId{ get; set; }
.... etc
}
我创建了这个方法来创建递归类别列表并加入文章,但它不起作用
private IEnumerable<Categories> GetCatList(int category)
{
return db.Categories.Where(x => x.parentId == category || x.id == category).Union(db.Categories.Where(x => x.parentId == category).SelectMany(y =>GetCatList( y.id)));
}
我使用了 AsHierarchy
var a = db.Categories.ToList().AsHierarchy(e => e.id, e => e.parentId,category);
catModel = (from prod in ArticleList()
join cats in a.ToList()
on prod.Category equals cats.Parent.Category
select prod).ToList();
再次没有成功...
如果有人有解决方案请告诉我。
【问题讨论】:
标签: c# entity-framework linq recursion