Up LINQ query examples

Projection operators

Last modified on August 15, 2012 17:21

Examples of the LINQ Select and SelectMany operators are shown below.  In the examples below _em1 is an EntityManager.

C#
[Description("This samples uses SELECT to get all Customers as Entity Objects.")]
public void LinqToEntities11() {
  var query = from c in _em1.Customers
              select c;
  Assert.IsTrue(query.Count() == 91);
}

[Description("This samples uses SELECT to get all Customer Contact Names as Strings.")]
public void LinqToEntities12() {
  var query = from c in _em1.Customers
              orderby c.ContactName
              select c.ContactName;
  var r = query.ToList();
  Assert.IsTrue(r.First() == "Alejandra Camino");
}

[Description("This samples uses SELECT to get all Customer Contact Names as an anonoymous type.")]
public void LinqToEntities13() {
  var query = from c in _em1.Customers
              orderby c.CompanyName
              select new { c.ContactName };
 
  var r = query.ToList();
  Assert.IsTrue(r.First().ContactName == "Maria Anders");
}

[Description("This sample uses SELECT to get Orders as anonymous type")]
public void LinqToEntities14() {
  var query = from o in _em1.Orders
              where o.Customer.Address.City == "London"
              orderby o.OrderDate
              select new { o };
 
  var r = query.ToList();
  Assert.IsTrue(r.Count() == 46);
  Assert.IsTrue(r.First().o.OrderDate == new DateTime(1996, 8, 26));
}

[Description("This sample uses SELECT to get all Orders and associated Customers as anonymous type")]
public void LinqToEntities15() {
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly;
  var query = from o in _em1.Orders
              where o.Customer.Address.City == "London"
              orderby o.Customer.CompanyName descending
              select new { o, o.Customer };

  var r = query.ToList();
  Assert.IsTrue(r.Count() == 46);
  Assert.IsTrue(r.First().Customer.CompanyName == "Seven Seas Imports");
}

[Description("This sample uses SELECTMANY to get all Orders for a Customer as a flat result")]
public void LinqToEntities16() {
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly;
  var query = from c in _em1.Customers
              where c.CustomerID == "ALFKI"
              from o in c.Orders
              orderby o.OrderDate
              select o;

  var r = query.ToList();
  Assert.IsTrue(r.Count() == 6);
  Assert.IsTrue(r.First().OrderDate == new DateTime(1997, 8, 25));
}

[Description("This sample uses SELECTMANY to get all Orders for a Customer as a flat result as a method query")]
public void LinqToEntities17() {
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly;
  var query = _em1.Customers.Where(cust => cust.CustomerID == "ALFKI")
      .SelectMany(cust => cust.Orders
          .OrderBy(o => o.OrderDate));

  var r = query.ToList();
  Assert.IsTrue(r.Count() == 6);
  Assert.IsTrue(r.First().OrderDate == new DateTime(1997, 8, 25));
}

[Description("This sample uses SELECTMANY to get all Orders for Customers in Denmark as a flat result")]
public void LinqToEntities18() {
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly;
  var query = from c in _em1.Customers
              where c.Address.Country == "Denmark"
              from o in c.Orders
              orderby o.OrderDate
              select o;

  var r = query.ToList();
  Assert.IsTrue(r.Count() == 18);
  Assert.IsTrue(r.First().OrderDate == new DateTime(1996, 10, 29));
}

[Description("This sample uses SELECTMANY to get all Orders for Customers in Denmark as a flat result as a method query")]
public void LinqToEntities19() {
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly;
  var query = _em1.Customers.Where(cust => cust.Address.Country == "Denmark")
      .SelectMany(cust => cust.Orders);

  var r = query.ToList();
  Assert.IsTrue(r.Count() == 18);
}


[Description("This sample uses SELECTMANY to get all Orders for Customers in Denmark as a flat result")]
public void LinqToEntities20x() {
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly;
  var query = from c in _em1.Customers
              where c.Address.Country == "Denmark"
              from o in c.Orders
              where o.Freight > 5
              orderby o.OrderDate
              select o;
 
  Assert.IsTrue(query.Count() == 17);
  Assert.IsTrue(query.First().OrderDate == new DateTime(1996, 10, 29));
}

[Description("This sample uses SELECTMANY to get all Orders for Customers in Denmark as an anonymous type containing the Orders and Customer flat result")]
public void LinqToEntities21x() {
  _em1.DefaultQueryStrategy = QueryStrategy.Normal.With(QueryInversionMode.Manual);
  var query = from c in _em1.Customers
              where c.Address.Country == "Denmark"
              orderby c.CompanyName
              from o in c.Orders
              where o.Freight > 5
              orderby o.OrderDate
              select new { c, o};
 
  Assert.IsTrue(query.Count() == 17);
  var x = query.First();
  Assert.IsTrue(query.First().o.OrderDate == new DateTime(1996, 10, 29));
}

[Description("This sample uses SELECTMANY to get all Orders for Customers in Denmark as a flat result using LINQ opeartors")]
public void LinqToEntities22() {
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly;
  var query = _em1
      .Customers
      .Where(cust => cust.Address.Country == "Denmark")
      .SelectMany(cust => cust.Orders
          .Where(o => o.Freight > 5)
          .OrderBy(o => o.OrderDate));
  var r = query.ToList();
  Assert.IsTrue(r.Count() == 17);
  Assert.IsTrue(r.First().OrderDate == new DateTime(1996, 10, 29));
}
VB
<Description("This samples uses SELECT to get all Customers as Entity Objects.")>
Public Sub LinqToEntities11()
 Dim query = From c In _em1.Customers
             Select c
  Assert.IsTrue(query.Count() = 91)
End Sub

<Description("This samples uses SELECT to get all Customer Contact Names as Strings.")>
Public Sub LinqToEntities12()
 Dim query = From c In _em1.Customers
              Order By c.ContactName
             Select c.ContactName
 Dim r = query.ToList()
  Assert.IsTrue(r.First() = "Alejandra Camino")
End Sub

<Description("This samples uses SELECT to get all Customer Contact Names as an anonoymous type.")>
Public Sub LinqToEntities13()
 Dim query = From c In _em1.Customers
              Order By c.CompanyName
             Select New With {Key c.ContactName}

 Dim r = query.ToList()
  Assert.IsTrue(r.First().ContactName = "Maria Anders")
End Sub

<Description("This sample uses SELECT to get Orders as anonymous type")>
Public Sub LinqToEntities14()
 Dim query = From o In _em1.Orders
              Where o.Customer.Address.City = "London"
              Order By o.OrderDate
             Select New With {Key o}

 Dim r = query.ToList()
  Assert.IsTrue(r.Count() = 46)
  Assert.IsTrue(r.First().o.OrderDate = New Date(1996, 8, 26))
End Sub

<Description("This sample uses SELECT to get all Orders and associated Customers as anonymous type")>
Public Sub LinqToEntities15()
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly
 Dim query = From o In _em1.Orders
              Where o.Customer.Address.City = "London"
              Order By o.Customer.CompanyName Descending
             Select New With {Key o, Key o.Customer}

 Dim r = query.ToList()
  Assert.IsTrue(r.Count() = 46)
  Assert.IsTrue(r.First().Customer.CompanyName = "Seven Seas Imports")
End Sub

<Description("This sample uses SELECTMANY to get all Orders for a Customer as a flat result")>
Public Sub LinqToEntities16()
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly
 Dim query = From c In _em1.Customers
              Where c.CustomerID = "ALFKI"
              From o In c.Orders
              Order By o.OrderDate
             Select o

 Dim r = query.ToList()
  Assert.IsTrue(r.Count() = 6)
  Assert.IsTrue(r.First().OrderDate = New Date(1997, 8, 25))
End Sub

<Description("This sample uses SELECTMANY to get all Orders for a Customer as a flat result as a method query")>
Public Sub LinqToEntities17()
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly
 Dim query = _em1.Customers.Where(Function(cust) cust.CustomerID = "ALFKI").SelectMany(Function(cust) cust.Orders.OrderBy(Function(o) o.OrderDate))

 Dim r = query.ToList()
  Assert.IsTrue(r.Count() = 6)
  Assert.IsTrue(r.First().OrderDate = New Date(1997, 8, 25))
End Sub

<Description("This sample uses SELECTMANY to get all Orders for Customers in Denmark as a flat result")>
Public Sub LinqToEntities18()
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly
 Dim query = From c In _em1.Customers
              Where c.Address.Country = "Denmark"
              From o In c.Orders
              Order By o.OrderDate
             Select o

 Dim r = query.ToList()
  Assert.IsTrue(r.Count() = 18)
  Assert.IsTrue(r.First().OrderDate = New Date(1996, 10, 29))
End Sub

<Description("This sample uses SELECTMANY to get all Orders for Customers in Denmark as a flat result as a method query")>
Public Sub LinqToEntities19()
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly
 Dim query = _em1.Customers.Where(Function(cust) cust.Address.Country = "Denmark").SelectMany(Function(cust) cust.Orders)

 Dim r = query.ToList()
  Assert.IsTrue(r.Count() = 18)
End Sub


<Description("This sample uses SELECTMANY to get all Orders for Customers in Denmark as a flat result")>
Public Sub LinqToEntities20x()
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly
 Dim query = From c In _em1.Customers
              Where c.Address.Country = "Denmark"
              From o In c.Orders
              Where o.Freight > 5
              Order By o.OrderDate
             Select o

  Assert.IsTrue(query.Count() = 17)
  Assert.IsTrue(query.First().OrderDate = New Date(1996, 10, 29))
End Sub

<Description("This sample uses SELECTMANY to get all Orders for Customers in Denmark as an anonymous type containing the Orders and Customer flat result")>
Public Sub LinqToEntities21x()
  _em1.DefaultQueryStrategy = QueryStrategy.Normal.With(QueryInversionMode.Manual)
 Dim query = From c In _em1.Customers
              Where c.Address.Country = "Denmark"
              Order By c.CompanyName
              From o In c.Orders
              Where o.Freight > 5
              Order By o.OrderDate
             Select New With {Key c, Key o}

  Assert.IsTrue(query.Count() = 17)
 Dim x = query.First()
  Assert.IsTrue(query.First().o.OrderDate = New Date(1996, 10, 29))
End Sub

<Description("This sample uses SELECTMANY to get all Orders for Customers in Denmark as a flat result using LINQ opeartors")>
Public Sub LinqToEntities22()
  _em1.DefaultQueryStrategy = QueryStrategy.DataSourceOnly
 Dim query = _em1.Customers.Where(Function(cust) cust.Address.Country = "Denmark").SelectMany(Function(cust) cust.Orders.Where(Function(o) o.Freight > 5).OrderBy(Function(o) o.OrderDate))
 Dim r = query.ToList()
  Assert.IsTrue(r.Count() = 17)
  Assert.IsTrue(r.First().OrderDate = New Date(1996, 10, 29))
End Sub
Tags: Query
Created by DevForce on February 18, 2011 03:49

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