This is the model:
Public Class Parent
Public Property Name As String
Public Property ID As Integer
Public Property child1 As New List(Of Child1)
End Class
Public Class Child1
Public Property ID As Integer
Public Property Name As String
Public Property Child2 As New List(Of Child2)
End Class
Public Class Child2
Public Property ID As Integer
Public Property Name As String
Public Property Child3 As New List(Of Child3)
End Class
Public Class Child3
Public Property ID As Integer
Public Property Name As String
Public Property Child4 As New List(Of Child4)
End Class
Public Class Child4
Public Property ID As Integer
Public Property Name As String
End Class
To include all tables in a query I have been using:
TheDataContext.Parent.Include("Child1").Include("Child1.Child2").Include("Child1.Child2.Child3").Include("Child1.Child2.Child3.Child4").Where(some condition).ToList()
So that's ridiculously messy and has no type checking. I did learn the following works if I only need to go one child table deep:
TheDataContext.Parent.Include(NameOf(Child1)).Where(some condition).ToList()
But if I try to apply the 'NameOf' technique on the next child level down like this
.Include(NameOf(Child1.Child2))...
That fails at runtime saying Parent does not declare a navigation property with the name Child1.Child2, which in retrospect makes sense, but I don't see how I would fix that. And if I use
.Include(NameOf(Parent.Child1.Child2))...
That fails in the editor saying reference to a non-shared member requires an object reference, which I understand, but I don't believe that's a solution, my model shouldn't be using Shared.
This SO Post suggests a lambda in the Include, with a Select that contains another lambda, which I understand in C#, but in vb.net the .include doesn't seem to even allow the first lambda. The following fails in the editor:
.include(function(x) x.Child1) or anything else in the lambda
Perhaps I'm missing an Imports, but short of that I simply don't know the syntax to do in vb.net that which seems so elegant in that SO post.
How do I convert over to type checking for all levels of children?
Added after applying suggestions and answers below:
Not disregarding Harald Coppoolse's excellent answer, this was how I converted the string-based .Include's to an .Include with type checking. While this was technically the answer to my question, as Harald's answer shows, an answer is not necessarily the right answer because there is likely an important question that hasn't been asked.
TheDataContext.Parent.Include(Function(a) a.child1.Select(Function(b) b.child2.Select(Function(c) c.child3.Select(Function(d) d.child4)))).ToList()