Beispiele für die Abfrageausdruckssyntax: Einschränkung (LINQ to Entities)
In den Beispielen in diesem Thema wird gezeigt, wie Sie die Where-Methode verwenden können, um das AdventureWorks Sales-Modell mithilfe von Abfrageausdruckssyntax abzufragen. Für das in den Beispielen verwendete "AdventureWorks Sales"-Modell wurde auf die Tabellen Contact, Address, Product, SalesOrderHeader und SalesOrderDetail der "AdventureWorks"-Beispieldatenbank zurückgegriffen.
Die Beispiele in diesem Thema verwenden die folgenden using/Imports-Anweisungen:
Option Explicit On
Option Strict On
Imports L2EExamplesVB.AdventureWorksModel
Imports System.Data.Objects
Imports System.Globalization
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using AdventureWorksModel;
using System.Globalization;
Weitere Informationen finden Sie unter Gewusst wie: Erstellen eines LINQ to Entities-Projekts in Visual Studio.
Where
Beispiel
Im folgenden Beispiel werden alle Onlinebestellungen zurückgegeben.
Using AWEntities As New AdventureWorksEntities
Dim orders As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader
Dim onlineOrders = _
From order In orders _
Where order.OnlineOrderFlag = True _
Select New With { _
.SalesOrderID = order.SalesOrderID, _
.OrderDate = order.OrderDate, _
.SalesOrderNumber = order.SalesOrderNumber _
}
For Each onlineOrder In onlineOrders
Console.WriteLine("Order ID: {0} Order date: {1:d} Order number: {2}", _
onlineOrder.SalesOrderID, _
onlineOrder.OrderDate, _
onlineOrder.SalesOrderNumber)
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<SalesOrderHeader> orders = AWEntities.SalesOrderHeader;
var onlineOrders =
from order in orders
where order.OnlineOrderFlag == true
select new
{
SalesOrderID = order.SalesOrderID,
OrderDate = order.OrderDate,
SalesOrderNumber = order.SalesOrderNumber
};
foreach (var onlineOrder in onlineOrders)
{
Console.WriteLine("Order ID: {0} Order date: {1:d} Order number: {2}",
onlineOrder.SalesOrderID,
onlineOrder.OrderDate,
onlineOrder.SalesOrderNumber);
}
}
Beispiel
Im folgenden Beispiel werden die Aufträge zurückgegeben, bei denen die Bestellmenge größer als 2 und kleiner als 6 ist.
Using AWEntities As New AdventureWorksEntities
Dim orders As ObjectQuery(Of SalesOrderDetail) = AWEntities.SalesOrderDetail
Dim query = _
From order In orders _
Where order.OrderQty > 2 And order.OrderQty < 6 _
Select New With { _
.SalesOrderID = order.SalesOrderID, _
.OrderQty = order.OrderQty _
}
For Each order In query
Console.WriteLine("Order ID: {0} Order quantity: {1}", _
order.SalesOrderID, order.OrderQty)
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<SalesOrderDetail> orders = AWEntities.SalesOrderDetail;
var query =
from order in orders
where order.OrderQty > 2 && order.OrderQty < 6
select new
{
SalesOrderID = order.SalesOrderID,
OrderQty = order.OrderQty
};
foreach (var order in query)
{
Console.WriteLine("Order ID: {0} Order quantity: {1}",
order.SalesOrderID, order.OrderQty);
}
}
Beispiel
Im folgenden Beispiel werden alle Produkte mit roter Farbe zurückgegeben.
Using AWEntities As New AdventureWorksEntities
Dim products As ObjectQuery(Of Product) = AWEntities.Product
Dim query = _
From product In products _
Where product.Color = "Red" _
Select New With { _
.Name = product.Name, _
.ProductNumber = product.ProductNumber, _
.ListPrice = product.ListPrice _
}
For Each product In query
Console.WriteLine("Name: {0}", product.Name)
Console.WriteLine("Product number: {0}", product.ProductNumber)
Console.WriteLine("List price: ${0}", product.ListPrice)
Console.WriteLine("")
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<Product> products = AWEntities.Product;
var query =
from product in products
where product.Color == "Red"
select new
{
Name = product.Name,
ProductNumber = product.ProductNumber,
ListPrice = product.ListPrice
};
foreach (var product in query)
{
Console.WriteLine("Name: {0}", product.Name);
Console.WriteLine("Product number: {0}", product.ProductNumber);
Console.WriteLine("List price: ${0}", product.ListPrice);
Console.WriteLine("");
}
}
Beispiel
Im folgenden Beispiel wird die Where-Methode verwendet, um Aufträge zu finden, die nach dem 1. Dezember 2003 eingegangen sind. Anschließend wird die order.SalesOrderDetail-Navigationseigenschaft verwendet, um die Details für jeden Auftrag abzurufen.
Using AWEntities As New AdventureWorksEntities
Dim orders As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader
Dim query = _
From order In orders _
Where order.OrderDate >= New DateTime(2003, 12, 1) _
Select order
Console.WriteLine("Orders that were made after December 1, 2003:")
For Each order In query
Console.WriteLine("OrderID {0} Order date: {1:d} ", _
order.SalesOrderID, order.OrderDate)
For Each orderDetail In order.SalesOrderDetail
Console.WriteLine(" Product ID: {0} Unit Price {1}", _
orderDetail.ProductID, orderDetail.UnitPrice)
Next
Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
ObjectQuery<SalesOrderHeader> orders = AWEntities.SalesOrderHeader;
IQueryable<SalesOrderHeader> query =
from order in orders
where order.OrderDate >= new DateTime(2003, 12, 1)
select order;
Console.WriteLine("Orders that were made after December 1, 2003:");
foreach (SalesOrderHeader order in query)
{
Console.WriteLine("OrderID {0} Order date: {1:d} ",
order.SalesOrderID, order.OrderDate);
foreach (SalesOrderDetail orderDetail in order.SalesOrderDetail)
{
Console.WriteLine(" Product ID: {0} Unit Price {1}",
orderDetail.ProductID, orderDetail.UnitPrice);
}
}
}