Loading data from database (without iteration required)

Description Article and Code Samples
Table of Contents
Description

What it is

DbContext API includes a method DbSet.Load() whcich can be used on DbSet to pull all the data from the database into memory.

Because Load is as an extension method on IQueryable<T>, it can be used to load the results of a LINQ query into memory.

Note

When using the DbSet.Load on a LINQ query it will bring the results of that query into memory but it does not remove the results of previous queries.

If a first query is loaded and then a second query is also loaded both results will be returned from Local.

Operations
Load all data of a DbSet from database into memory
How to do it
using (var context = new BreakAwayContext())
{
    context.Destinations.Load();

    var count = context.Destinations.Local.Count;
    Console.WriteLine("Destinations in memory: {0}", count);
}
Load LINQ query results into memory
How to do it
 using (var context = new BreakAwayContext())
{
    var query = from d in context.Destinations
                where d.Country == "Australia"
                select d;

    query.Load();

    var count = context.Destinations.Local.Count;
    Console.WriteLine("Aussie destinations in memory: {0}", count);
}