递归选择所有子ID并使用Linq连接一个字符串

时间:2014-11-28 09:29:57

标签: c# linq linq-to-entities

我的类别模型如下所示:

public class Category
{
  public int Id { get; set; }
  public int ParentCategoryId { get; set; }
  public string ChildIDs { get; set; }
}

我想递归选择所有子ID并加入像这样的ChildID: 12,13,17,31

这是我的Linq查询,但它返回 1级 childern:

from cg in db.Categories
where cg.Id == id
join urc in
    (from x in db.UrlRecords where x.EntityName == "Category" select x) on cg.Id equals urc.EntityId into urccg
from f in urccg.DefaultIfEmpty()
select new
{
    cg,
    f,
    ParentCategoryId = cg.ParentCategoryId,
    ChildIds = string.Join(",", (from x in db.Categories where x.ParentCategoryId == cg.Id select x.Id))
}

0 个答案:

没有答案