Linq extension method, how to find child in collection recursive

前端 未结 6 992
无人共我
无人共我 2021-02-07 14:58

I\'m already familiar with Linq but have little understanding of extension methods I\'m hoping someone can help me out.

So I have this hierarchical collection pseudo cod

6条回答
  •  萌比男神i
    2021-02-07 15:23

    static IEnumerable FindProductById(this IEnumerable source, int id) 
    {
        return source.FirstOrDefault(product => product.Id = id) ?? source.SelectMany(product => product.Children).FindProductById(id);
    }
    

提交回复
热议问题