Up LINQ query examples

Inheritance operators

Last modified on September 14, 2012 20:34

Examples of the LINQ OfType operator are shown below.  In the examples below _em1 is an EntityManager.

C#
[Description("Select all products, both active and discontinued products, and show the type.")]
public void LinqToEntities77() {
  var query = _em1
      .Products
      .Select(p => p);

  var query2 = query
   // force local execution to show local type
     .AsEnumerable()
      .Select(p => new { type = p.GetType().ToString(), prod = p });

  StringBuilder msg = new StringBuilder();
 foreach (var aProduct in query2) {
    msg.Append("(" + aProduct.type + ")  " + aProduct.prod.ProductName + Environment.NewLine);
  }
 // set break point and inspect msg.ToString() as desired

  var query3 = query2.OrderBy(p => p.prod.ProductName);
  Assert.IsTrue(query3.First().prod.ProductName == "Alice Mutton");
}

[Description("Select only discontinued products.")]
public void LinqToEntities78() {
  var query = _em1.Products.OfType<DiscontinuedProduct>().Select(p => p);
  var r = query.ToList();
  Assert.IsTrue(r.Count() == 8);
}

[Description("Select only products, which will reutrn all Products and subtypes of Products (DiscontinuedProducts and ActiveProducts).")]
public void LinqToEntities79() {
  var query = _em1.Products.OfType<Product>().Select(p => p);
  Assert.IsTrue(query.Count() == 77);
}
[Description("Select only discontinued products.")]
public void LinqToEntities80()  {
 // Similar to LinqToEntities78; no .Select clause
 var query = _em1.Products.OfType<DiscontinuedProduct>();
  Assert.IsTrue(query.Count() == 8);
}

[TestMethod]
[Description("Select only discontinued products.")]
public void LinqToEntities81() {
  var query = _em1.Products
      .Where(p => p is DiscontinuedProduct);
  Assert.IsTrue(query.Count() == 8);
}

[Description("Select all current employees.")]
public void LinqToEntities87() {
  var query = _em1.Employees.OfType<CurrentEmployee>()
      .ToList().Select(p => new { type = p.GetType().ToString(), p });
  Assert.IsTrue(query.Count() >= 8);
 int lastId = query.OrderBy(e => e.p.EmployeeID).Last().p.EmployeeID;
  var query2 = query.OrderByDescending(e => e.p.EmployeeID);
  Assert.IsTrue(query2.First().p.EmployeeID == lastId);
}
VB
<Description("Select all products, both active and discontinued products, and show the type.")>
Public Sub LinqToEntities77()
 Dim query = _em1.Products.Select(Function(p) p)

' force local execution to show local type
 Dim query2 = query.AsEnumerable().Select(Function(p) New With {Key .type = p.GetType().ToString(), Key .prod = p})

 Dim msg As New StringBuilder()
 For Each aProduct In query2
msg.Append("(" & aProduct.type & ")  " & aProduct.prod.ProductName & Environment.NewLine)
 Next aProduct
 ' set break point and inspect msg.ToString() as desired

 Dim query3 = query2.OrderBy(Function(p) p.prod.ProductName)
  Assert.IsTrue(query3.First().prod.ProductName = "Alice Mutton")
End Sub

<Description("Select only discontinued products.")>
Public Sub LinqToEntities78()
 Dim query = _em1.Products.OfType(Of DiscontinuedProduct)().Select(Function(p) p)
 Dim r = query.ToList()
  Assert.IsTrue(r.Count() = 8)
End Sub

<Description("Select only products, which will reutrn all Products and subtypes of Products (DiscontinuedProducts and ActiveProducts).")>
Public Sub LinqToEntities79()
 Dim query = _em1.Products.OfType(Of Product)().Select(Function(p) p)
  Assert.IsTrue(query.Count() = 77)
End Sub
<Description("Select only discontinued products.")>
Public Sub LinqToEntities80()
 ' Similar to LinqToEntities78; no .Select clause
 Dim query = _em1.Products.OfType(Of DiscontinuedProduct)()
  Assert.IsTrue(query.Count() = 8)
End Sub

<TestMethod, Description("Select only discontinued products.")>
Public Sub LinqToEntities81()
 Dim query = _em1.Products.Where(Function(p) TypeOf p Is DiscontinuedProduct)
  Assert.IsTrue(query.Count() = 8)
End Sub

<Description("Select all current employees.")>
Public Sub LinqToEntities87()
 Dim query = _em1.Employees.OfType(Of CurrentEmployee)().ToList().Select(Function(p) New With {Key .type = p.GetType().ToString(), Key p})
  Assert.IsTrue(query.Count() >= 8)
 Dim lastId As Integer = query.OrderBy(Function(e) e.p.EmployeeID).Last().p.EmployeeID
 Dim query2 = query.OrderByDescending(Function(e) e.p.EmployeeID)
  Assert.IsTrue(query2.First().p.EmployeeID = lastId)
End Sub
Tags: Query
Created by DevForce on February 18, 2011 04:32

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