Includes

Investor Services edited this page Dec 5, 2017 · 5 revisions

Including DataAccessObject properties

By default, accessing a property that is also a DataAccessObject will cause an additional SELECT query to the database. If you want to automatically include certain properties, Shoalinq will automatically perform the current joins. You can include chains of properties to any depth you like. Including a property at depth N will implicitly include properties at depth 0 - N.

Examples

var person = model.People.Include(c => c.BestFriend).First();

// Will not cause an additional query
Console.WriteLine(person.BestFriend.Name);
var person = model.People.Include(c => c.BestFriend.BorrowedBook).ToList();

// Will not cause an additional query
Console.WriteLine(person.BestFriend.BorrowedBook.PublisherName);

Including RelatedDataAccessObjects properties

It's possible to include a RelatedDataAccessObjects<T> property also using the Include method. Because the property remains a RelatedDataAccessObjects<T> property that implements IQueryable<T>, accessing the property after a query will still perform an additional query. To access the pre-loaded list of related objects use the Items() method on the RelatedDataAccessObjects<T> class.

var book = model.Books.Include(c => c.Borrowers).First();

// Will cause an additional query
Console.WriteLine(book.Borrowers.First().Name);

// Will not cause an additional query
Console.WriteLine(book.Borrowers.Items().First().Name);

Deep Includes

You can perform deep/nested includes of items within a collection by using the IncludedItems() extension method.

var book = model
    .Books
    .Include(c => c.Borrowers.IncludedItems().BestFriend)
    .First();
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.