Recursive LINQ query: select item and all children with subchildren

前端 未结 3 1916
梦谈多话
梦谈多话 2020-11-30 03:26

Is there any way to write a LINQ (or procedural style) query, that can select an item and all children with one query? I have entity:

public class Comment
{
         


        
3条回答
  •  孤街浪徒
    2020-11-30 04:14

    IEnumerable GetChild(int id)
    {
        return table.Where(x => x.ParentID == id || x.Id== id)
                    .Union(table.Where(x => x.ParentID == id)
                                .SelectMany(y => GetChild(y.Id))
        );
    }
    

提交回复
热议问题