Uredi

Deli z drugimi prek


Formulate Projections

The following examples show how the select statement in C# and Select statement in Visual Basic can be combined with other features to form query projections.

Example 1

The following example uses the Select clause in Visual Basic (select clause in C#) to return a sequence of contact names for Customers.

var nameQuery =
    from cust in db.Customers
    select cust.ContactName;
Dim nameQuery = From cust In db.Customers _
                Select cust.ContactName

Example 2

The following example uses the Select clause in Visual Basic (select clause in C#) and anonymous types to return a sequence of contact names and telephone numbers for Customers.

var infoQuery =
    from cust in db.Customers
    select new { cust.ContactName, cust.Phone };
Dim infoQuery = From cust In db.Customers _
                Select cust.ContactName, cust.Phone

Example 3

The following example uses the Select clause in Visual Basic (select clause in C#) and anonymous types to return a sequence of names and telephone numbers for employees. The FirstName and LastName fields are combined into a single field (Name), and the HomePhone field is renamed to Phone in the resulting sequence.

var info2Query =
    from emp in db.Employees
    select new
    {
        Name = emp.FirstName + " " + emp.LastName,
        Phone = emp.HomePhone
    };
Dim info2Query = From emp In db.Employees _
                 Select Name = emp.FirstName & " " & emp.LastName, _
             Phone = emp.HomePhone

Example 4

The following example uses the Select clause in Visual Basic (select clause in C#) and anonymous types to return a sequence of all ProductIDs and a calculated value named HalfPrice. This value is set to the UnitPrice divided by 2.

var specialQuery =
    from prod in db.Products
    select new { prod.ProductID, HalfPrice = prod.UnitPrice / 2 };
Dim specialQuery = From prod In db.Products _
                   Select prod.ProductID, HalfPrice = CDec(prod.UnitPrice) / 2

Example 5

The following example uses the Select clause in Visual Basic (select clause in C#) and a conditional statement to return a sequence of product name and product availability.

var prodQuery =
    from prod in db.Products
    select new
    {
        prod.ProductName,
        Availability =
            prod.UnitsInStock - prod.UnitsOnOrder < 0
        ? "Out Of Stock" : "In Stock"
    };
Dim prodQuery = From prod In db.Products _
                Select prod.ProductName, Availability = _
                    If(prod.UnitsInStock - prod.UnitsOnOrder < 0, _
                    "Out Of Stock", "In Stock")

Example 6

The following example uses a Visual Basic Select clause (select clause in C#) and a known type (Name) to return a sequence of the names of employees.

public class Name
{
    public string FirstName = "";
    public string LastName = "";
}

 void empMethod()
 {
 Northwnd db = new Northwnd(@"c:\northwnd.mdf");
 var empQuery =
     from emp in db.Employees
     select new Name
     {
         FirstName = emp.FirstName,
         LastName = emp.LastName
     };
}
Public Class Name
    Public FirstName As String
    Public LastName As String
End Class

Dim db As New Northwnd("c:\northwnd.mdf")
Dim empQuery = From emp In db.Employees _
               Select New Name With {.FirstName = emp.FirstName, .LastName = _
                   emp.LastName}

Example 7

The following example uses Select and Where in Visual Basic (select and where in C#) to return a filtered sequence of contact names for customers in London.

var contactQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust.ContactName;
Dim contactQuery = _
    From cust In db.Customers _
    Where cust.City = "London" _
    Select cust.ContactName

Example 8

The following example uses a Select clause in Visual Basic (select clause in C#) and anonymous types to return a shaped subset of the data about customers.

var custQuery =
    from cust in db.Customers
    select new
    {
        cust.CustomerID,
        CompanyInfo = new { cust.CompanyName, cust.City, cust.Country },
        ContactInfo = new { cust.ContactName, cust.ContactTitle }
    };
Dim custQuery = From cust In db.Customers _
                Select cust.CustomerID, CompanyInfo = New With {cust.CompanyName, _
                    cust.City, cust.Country}, ContactInfo = _
                    New With {cust.ContactName, cust.ContactTitle}

Example 9

The following example uses nested queries to return the following results:

  • A sequence of all orders and their corresponding OrderIDs.

  • A subsequence of the items in the order for which there is a discount.

  • The amount of money saved if the cost of shipping is not included.

var ordQuery =
    from ord in db.Orders
    select new
    {
        ord.OrderID,
        DiscountedProducts =
            from od in ord.OrderDetails
            where od.Discount > 0.0
            select od,
        FreeShippingDiscount = ord.Freight
    };
Dim ordQuery = From ord In db.Orders _
               Select ord.OrderID, DiscountedProducts = _
               (From od In ord.OrderDetails _
                Where od.Discount > 0.0 _
                Select od), _
           FreeShippingDiscount = ord.Freight

See also