eager loading using include method in entity framework

Data shaping is an important benefit of using Include method. Include is a query builder method and we can apply it to the ObjectQuery or ObjectSet. In the given query, the context.Customer results an Objectset. So, we have used Include in the LINQ query. The argument for Include method can be any navigation property. This refers to as Eager loading.

Note that we can use Include method only when the returning type can be either ObjectQuery or ObjectSet. We cannot use Projections with Include method.

The below query uses Include method with no Projections. The query returns only the primary entity of the query - Customers. Note that we can also access the properties of the Include entities. In the query given below, we have used total field of order to limit the customers.

using (var context = new SalesOrderManagementEntities())
	var Customers = from c in context.Customer.Include("Order")
					where c.Order.Any((o)=>o.Total>100)
					select c;
	foreach (var Customer in Customers)
		Console.WriteLine(String.Format("{0}", Customer.Name));