Up LINQ query examples

Include operators

Last modified on August 15, 2012 17:22

Examples of using DevForce Include operator are shown below.  In the examples below _em1 is an EntityManager.

C#
[Description("Load OrderDetails with Orders ")]
public void LinqToEntities94() {
  var query0 = _em1.Orders.Include("OrderDetails")
      .Where(c => c.Customer.Address.City == "London").Select(o => o);

  var query1 = query0.OrderBy(o => o.OrderID);
  var r1 = query1.ToList();

  var o1 = query1.First();
  var count1 = o1.OrderDetails.Count();
  Assert.IsTrue(count1 == 2);
}

[Description("Load OrderDetails and Products with Orders ")]
public void LinqToEntities95() {
  var query = _em1.Orders
      .Include("OrderDetails")
      .Include("OrderDetails.Product")
      .Take(3).Select(o => o);

  var query2 = query.OrderByDescending(o => o.OrderID);
  var r2 = query2.ToList();
  var p = r2.First().OrderDetails.First().Product;
  Assert.IsNotNull(p);
  Assert.IsFalse(p.EntityAspect.IsNullOrPendingEntity);
}
VB
<Description("Load OrderDetails with Orders ")>
Public Sub LinqToEntities94()
 Dim query0 = _em1.Orders.Include("OrderDetails").Where(Function(c) _
    c.Customer.Address.City = "London").Select(Function(o) o)

 Dim query1 = query0.OrderBy(Function(o) o.OrderID)
 Dim r1 = query1.ToList()

 Dim o1 = query1.First()
 Dim count1 = o1.OrderDetails.Count()
  Assert.IsTrue(count1 = 2)
End Sub

<Description("Load OrderDetails and Products with Orders ")>
Public Sub LinqToEntities95()
 Dim query = _em1.Orders.Include("OrderDetails").Include _
    ("OrderDetails.Product").Take(3).Select(Function(o) o)

 Dim query2 = query.OrderByDescending(Function(o) o.OrderID)
 Dim r2 = query2.ToList()
 Dim p = r2.First().OrderDetails.First().Product
  Assert.IsNotNull(p)
  Assert.IsFalse(p.EntityAspect.IsNullOrPendingEntity)
End Sub
Tags: Query
Created by DevForce on February 18, 2011 08:31

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2020 IdeaBlade