How do I select recursive nested entities using LINQ to Entity

后端 未结 3 1488
臣服心动
臣服心动 2020-12-31 12:56

I have an entity called Category and the entity contains a IEnumerable called ChildCategories. A category can have these child categories which can have it\'s own child cate

3条回答
  •  情话喂你
    2020-12-31 13:41

    There where some problems with casperOnes code. This works:

    public static IEnumerable Flatten(this IEnumerable source, Func> childrenSelector)
        {
            // Do standard error checking here.
    
            // Cycle through all of the items.
            foreach (T item in source)
            {
                // Yield the item.
                yield return item;
    
                // Yield all of the children.
                foreach (T child in childrenSelector(item).Flatten(childrenSelector))
                {
                    // Yield the item.
                    yield return child;
                }
            }
        }
    

提交回复
热议问题