Share via


如何:投影查询结果(WCF 数据服务)

投影提供减少查询返回的数据量的机制,方法是指定在响应中仅返回某个实体的某些属性。 可以通过在 LINQ 查询中使用 $select 查询选项或使用 select 子句(在 Visual Basic 中为 Select)对 WCF 数据服务 查询结果执行投影。 有关更多信息,请参见查询数据服务(WCF 数据服务)

本主题中的示例使用 Northwind 示例数据服务和自动生成的客户端数据服务类。 此服务和这些客户端数据类是在完成 WCF 数据服务快速入门时创建的。

示例

下面的示例演示一个 LINQ 查询,该查询将 Customers 实体投影到新的 CustomerAddress 类型,该类型仅包含特定地址属性以及标识属性。 此 CustomerAddress 类是在客户端上定义的并且具有客户端库可将它识别为实体类型的特性。

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Define an anonymous LINQ query that projects the Customers type into 
' a CustomerAddress type that contains only address properties.
Dim query = From c In context.Customers _
                Where c.Country = "Germany" _
                Select New CustomerAddress With { _
                    .CustomerID = c.CustomerID, _
                    .Address = c.Address, _
                    .City = c.City, _
                    .Region = c.Region, _
                    .PostalCode = c.PostalCode, _
                    .Country = c.Country}

Try
    ' Enumerate over the query result, which is executed implicitly.
    For Each item In query
        ' Modify the address and mark the object as updated.
        item.Address += " #101"
        context.UpdateObject(item)

        ' Write out the current values.
        Console.WriteLine("Customer ID: {0} " & vbCrLf & "Street: {1} " _
                & vbCrLf & "City: {2} " & vbCrLf & "State: {3} " & vbCrLf & "Zip Code: {4}" _
                & vbCrLf & "Country: {5}", _
                item.CustomerID, item.Address, item.City, item.Region, _
                item.PostalCode, item.Country)
    Next

    ' Save changes to the data service.
    context.SaveChanges()
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

// Define an anonymous LINQ query that projects the Customers type into 
// a CustomerAddress type that contains only address properties.
var query = from c in context.Customers
            where c.Country == "Germany"
            select new CustomerAddress { 
                CustomerID = c.CustomerID, 
                Address = c.Address, 
                City = c.City, 
                Region = c.Region,
                PostalCode = c.PostalCode, 
                Country = c.Country};

try
{
    // Enumerate over the query result, which is executed implicitly.
    foreach (var item in query)
    {
        // Modify the address and mark the object as updated.
        item.Address += " #101";
        context.UpdateObject(item);

        // Write out the current values.
        Console.WriteLine("Customer ID: {0} \r\nStreet: {1} "
            + "\r\nCity: {2} \r\nState: {3} \r\nZip Code: {4} \r\nCountry: {5}", 
            item.CustomerID, item.Address, item.City, item.Region, 
            item.PostalCode, item.Country);
    }

    // Save changes to the data service.
    context.SaveChanges();
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

下面的示例演示一个 LINQ 查询,该查询将返回的 Customers 实体投影到新的 CustomerAddressNonEntity 类型,该类型仅包含特定地址属性,不包含标识属性。 此 CustomerAddressNonEntity 类是在客户端上定义的,但不具有作为实体类型的特性。

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Define an anonymous LINQ query that projects the Customers type into 
' a CustomerAddress type that contains only address properties.
Dim query = From c In context.Customers _
                Where c.Country = "Germany" _
                Select New CustomerAddressNonEntity With _
                {.CompanyName = c.CompanyName, _
                    .Address = c.Address, _
                    .City = c.City, _
                    .Region = c.Region, _
                    .PostalCode = c.PostalCode, _
                    .Country = c.Country}

Try
    ' Enumerate over the query result, which is executed implicitly.
    For Each item In query
        item.Address += "Street"

        Console.WriteLine("Company name: {0} " & vbNewLine & "Street: {1} " _
            & "" & vbNewLine & "City: {2} " & vbNewLine & "State: {3} " & vbNewLine & _
            "Zip Code: {4} " & vbNewLine & "Country: {5}", _
            item.CompanyName, item.Address, item.City, item.Region, _
            item.PostalCode, item.Country)
    Next
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

// Define an anonymous LINQ query that projects the Customers type into 
// a CustomerAddress type that contains only address properties.
var query = from c in context.Customers
            where c.Country == "Germany"
            select new CustomerAddressNonEntity
            {
                CompanyName = c.CompanyName, 
                Address = c.Address,
                City = c.City, 
                Region = c.Region,
                PostalCode = c.PostalCode, 
                Country = c.Country
            };

try
{
    // Enumerate over the query result, which is executed implicitly.
    foreach (var item in query)
    {
        item.Address += "Street";

        Console.WriteLine("Company name: {0} \nStreet: {1} "
            + "\nCity: {2} \nState: {3} \nZip Code: {4} \nCountry: {5}",
            item.CompanyName, item.Address, item.City, item.Region,
            item.PostalCode, item.Country);
    }     
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

下面的示例演示在上述示例中使用的 CustomerAddress CustomerAddressNonEntity 类型的定义。

<DataServiceKey("CustomerID")> _
Partial Public Class CustomerAddress
    Private _customerID As String
    Private _address As String
    Private _city As String
    Private _region As String
    Private _postalCode As String
    Private _country As String

    Public Property CustomerID() As String
        Get
            Return Me._customerID
        End Get
        Set(ByVal value As String)
            Me._customerID = Value
        End Set
    End Property
    Public Property Address() As String
        Get
            Return Me._address
        End Get
        Set(ByVal value As String)
            Me._address = Value
        End Set
    End Property
    Public Property City() As String
        Get
            Return Me._city
        End Get
        Set(ByVal value As String)
            Me._city = Value
        End Set
    End Property
    Public Property Region() As String
        Get
            Return Me._region
        End Get
        Set(ByVal value As String)
            Me._region = Value
        End Set
    End Property
    Public Property PostalCode() As String
        Get
            Return Me._postalCode
        End Get
        Set(ByVal value As String)
            Me._postalCode = Value
        End Set
    End Property
    Public Property Country() As String
        Get
            Return Me._country
        End Get
        Set(ByVal value As String)
            Me._country = value
        End Set
    End Property
End Class
Public Class CustomerAddressNonEntity
    Private _companyName As String
    Private _address As String
    Private _city As String
    Private _region As String
    Private _postalCode As String
    Private _country As String

    Public Property CompanyName() As String
        Get
            Return Me._companyName
        End Get
        Set(ByVal value As String)
            Me._companyName = value
        End Set
    End Property
    Public Property Address() As String
        Get
            Return Me._address
        End Get
        Set(ByVal value As String)
            Me._address = Value
        End Set
    End Property
    Public Property City() As String
        Get
            Return Me._city
        End Get
        Set(ByVal value As String)
            Me._city = Value
        End Set
    End Property
    Public Property Region() As String
        Get
            Return Me._region
        End Get
        Set(ByVal value As String)
            Me._region = Value
        End Set
    End Property
    Public Property PostalCode() As String
        Get
            Return Me._postalCode
        End Get
        Set(ByVal value As String)
            Me._postalCode = Value
        End Set
    End Property
    Public Property Country() As String
        Get
            Return Me._country
        End Get
        Set(ByVal value As String)
            Me._country = value
        End Set
    End Property
End Class
[DataServiceKey("CustomerID")]
public partial class CustomerAddress
{
    private string _customerID;
    private string _address;
    private string _city;
    private string _region;
    private string _postalCode;
    private string _country;

    public string CustomerID
    {
        get
        {
            return this._customerID;
        }

        set
        {
            this._customerID = value;
        }
    }
    public string Address
    {
        get
        {
            return this._address;
        }
        set
        {
            this._address = value;
        }
    }
    public string City
    {
        get
        {
            return this._city;
        }
        set
        {
            this._city = value;
        }
    }
    public string Region
    {
        get
        {
            return this._region;
        }
        set
        {
            this._region = value;
        }
    }
    public string PostalCode
    {
        get
        {
            return this._postalCode;
        }
        set
        {
            this._postalCode = value;
        }
    }
    public string Country
    {
        get
        {
            return this._country;
        }
        set
        {
            this._country = value;
        }
    }
}

public class CustomerAddressNonEntity
{
    private string _companyName;
    private string _address;
    private string _city;
    private string _region;
    private string _postalCode;
    private string _country;

    public string CompanyName
    {
        get
        {
            return this._companyName;
        }
        set
        {
            this._companyName = value;
        }
    }
    public string Address
    {
        get
        {
            return this._address;
        }
        set
        {
            this._address = value;
        }
    }
    public string City
    {
        get
        {
            return this._city;
        }
        set
        {
            this._city = value;
        }
    }
    public string Region
    {
        get
        {
            return this._region;
        }
        set
        {
            this._region = value;
        }
    }
    public string PostalCode
    {
        get
        {
            return this._postalCode;
        }
        set
        {
            this._postalCode = value;
        }
    }
    public string Country
    {
        get
        {
            return this._country;
        }
        set
        {
            this._country = value;
        }
    }
}