How to return both parent and child using LINQ against 1 table

前端 未结 7 821
隐瞒了意图╮
隐瞒了意图╮ 2021-01-19 17:10

Been looking for a solution for this but haven\'t been able to find one so far.

I\'m fairly sure its possible with one linq call but having trouble working it out.

7条回答
  •  半阙折子戏
    2021-01-19 18:05

    I was going to use a GroupJoin, but this should satisfy your requirement.

     var query = dataContext.YourTable.Where(x => x.ValidFlag == 1 &&
     (x.ParentId == null ||
        dataContext.YourTable.Where( y => y.ParentId == x.Id)
        .First().ValidFlag == 1))
     .ToList();
                          .
    

提交回复
热议问题