1

I am trying to optimize some code here and even to make it better, because at the moment I just reach the 8ΒΊ level of child in my entity framework property tree.

I am doing the following:

var categories = CategoryRepository.Query(cat => cat.CategoryParent == null).
                            Include(firstLevel => firstLevel.CategoryChilds.
                                                Select(secondLevel => secondLevel.CategoryChilds.
                                                        Select(thirdLevel => thirdLevel.CategoryChilds.
                                                            Select(forthLevel => forthLevel.CategoryChilds.
                                                                Select(fifthLevel => fifthLevel.CategoryChilds.
                                                                        Select(sixthLevel => sixthLevel.CategoryChilds.
                                                                                Select(seventhLevel => seventhLevel.CategoryChilds))))))).Select().ToList();

As you might know, this is ULTRA KILL! And besides that, I am just reaching a specific level of child tree, I want to go deeper, till I don't find more childs, is that possible using some recursion?

Thank you very much! Have a great day!

Chandan Rai
  • 9,879
  • 2
  • 20
  • 28
TiagoM
  • 3,458
  • 4
  • 42
  • 83
  • 1
    please see if this helps http://stackoverflow.com/a/21262626/1530987 – Chandan Rai Feb 04 '17 at 09:26
  • 1
    I think this is basically the same as http://stackoverflow.com/questions/41894751/is-recursive-query-possible-in-linq/41909322#41909322. Hence `var categories = CategoryRepository.Query().AsEnumerable().Where(cat => cat.CategoryParent == null).ToList();` should do the trick. The only requirement is to have tracking enabled (i.e. no `AsNoTracking()` used). – Ivan Stoev Feb 04 '17 at 10:42
  • Thank you guys for your both answers! I will check them out and choose one, maybe you can write your own answer and I accept it? thank you ;) – TiagoM Feb 04 '17 at 15:53

0 Answers0