Data Access has been discontinued. Please refer to this page for more information.

How to: Execute a Query that Returns a Primitive Type

This topic provides examples of how to execute queries that return a primitive type.

To run the code in this example, you need a fluent model based on the Northwind database.

The following code is the LINQ example.

using (FluentModel dbContext = new FluentModel())
{
   IQueryable<Int32> orderIdQuery = 
        from order in dbContext.Orders
        where order.CustomerID == "ALFKI" || order.CustomerID == null
        select order.OrderID;

   // Iterate through the collection of values.
   foreach (Int32 id in orderIdQuery)
   {
       Console.WriteLine("{0}", id);
   }

   IQueryable<DateTime?> orderDateQuery = 
        from order in dbContext.Orders
        where order.CustomerID == "ALFKI" || order.CustomerID == null
        select order.OrderDate;

   // Iterate through the collection of values.
   foreach (DateTime? orderDate in orderDateQuery)
   {
       string orderDateMessage = "OrderDate not set";
       if (orderDate != null)
       {
           orderDateMessage = orderDate.ToString();
       }
       Console.WriteLine("Ship Date: {0}.", orderDateMessage);
   }
}
Using dbContext As New FluentModel()
 Dim orderIdQuery As IQueryable(Of Int32) = From order In dbContext.Orders
              Where order.CustomerID = "ALFKI" OrElse order.CustomerID Is Nothing
              Select order.OrderID
 ' Iterate through the collection of values.
 For Each id As Int32 In orderIdQuery
  Console.WriteLine("{0}", id)
 Next id
 Dim orderDateQuery As IQueryable(Of Date?) = From order In dbContext.Orders
                Where order.CustomerID = "ALFKI" OrElse order.CustomerID Is Nothing
                Select order.OrderDate
 ' Iterate through the collection of values.
 For Each orderDate? As Date In orderDateQuery
  Dim orderDateMessage As String = "OrderDate not set"
  If orderDate IsNot Nothing Then
   orderDateMessage = orderDate.ToString()
  End If
  Console.WriteLine("Ship Date: {0}.", orderDateMessage)
 Next orderDate
End Using

See Also

How to: Return a Specific Object Using its Key
How to: Order Two Unionized Queries
How to: Page Through Query Results