Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to Sql - Loading Child Entities Without Using DataLoadOptions?

Is it possible to load child entities in a single query without using DataLoadOptions?

I am using one data context per request in an asp.net web application and trying to get around the linq to sql limitation of not being able to change dataloadoptions once a query has been executed.

Thanks.

like image 842
user24358 Avatar asked Oct 26 '08 19:10

user24358


2 Answers

If you don't mind the link to the data context, as you say you don't, you could write a stored procedure that returns multiple results that map to your objects. Read more about it here.

like image 139
Omer van Kloeten Avatar answered Oct 16 '22 08:10

Omer van Kloeten


I found the following vb.net example which manually populates child entities from an IMultipleResults type:

   Public Function GetSubjectsWithBooks() As List(Of Subject)
        Dim results As IMultipleResults = Me.GetSubjectAndBooks
        Dim Subjects = results.GetResult(Of Subject).ToList
        Dim Books = results.GetResult(Of Book).ToList
        For Each s In Subjects
            Dim thisId As Guid = s.ID
            s.FetchedBooks = (From b In Books Where b.SubjectId = thisId).ToList
        Next
        Return Subjects
    End Function

This was taken from a sample project written by Jim Wooley (one of the Link in Action authors) which can be found at:http://www.thinqlinq.com/Downloads/LinqToSqlBeyondTheBasics.zip

Omer, is this the technique you were referring to?

like image 1
user24358 Avatar answered Oct 16 '22 09:10

user24358