Uredi

Deli z drugimi prek


LinqDataSource.Select Property

Definition

Gets or sets the properties and calculated values that are included in the retrieved data.

public:
 property System::String ^ Select { System::String ^ get(); void set(System::String ^ value); };
public string Select { get; set; }
member this.Select : string with get, set
Public Property Select As String

Property Value

A string that is used to create the Select clause.

Examples

The following example shows how to set the Select property to a subset of properties in the data object.

<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    Select="new(Name, Category, Price)"
    ID="LinqDataSource1" 
    runat="server">
</asp:LinqDataSource>
<asp:GridView 
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    runat="server">
</asp:GridView>
<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    Select="new(Name, Category, Price)"
    ID="LinqDataSource1" 
    runat="server">
</asp:LinqDataSource>
<asp:GridView 
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    runat="server">
</asp:GridView>

The following example shows how to retrieve values in a property that has been used to group data, and to retrieve the average for the grouped records. The Key keyword contains the property that has been used for grouping data. In this example, Key will contain the shared values from the ProductCategory property.

<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    GroupBy="Category"
    Select="new(Key as ProductCategory, 
            Average(Price) as AvePrice)"
    ID="LinqDataSource1" 
    runat="server">
</asp:LinqDataSource>
<asp:GridView 
    AllowPaging="true"
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    runat="server">
</asp:GridView>
<asp:LinqDataSource 
    ContextTypeName="ExampleDataContext" 
    TableName="Products" 
    GroupBy="Category"
    Select="new(Key as ProductCategory, 
            Average(Price) as AvePrice)"
    ID="LinqDataSource1" 
    runat="server">
</asp:LinqDataSource>
<asp:GridView 
    AllowPaging="true"
    DataSourceID="LinqDataSource1"
    ID="GridView1" 
    runat="server">
</asp:GridView>

Remarks

If you do not set the Select property, the LinqDataSource control retrieves all the properties (data columns) from the data object. You use the Select property to retrieve a subset of properties from the object or to calculate new values when the data is being retrieved.

You specify which properties to retrieve by setting the Select property to the names of the properties that you want to include in the returned data.

If you retrieve a subset of properties from the data object, the LinqDataSource control dynamically creates a class that contains only the properties that you specify in the Select property. A class is also created dynamically if you calculate properties that are not properties in the data object. In these cases, the object returned from the query is not an instance of the class that is specified in the TableName property. Instead, you must use the new method in the Select property to indicate that a new class is returned from the query. For example, to return only the FirstName and LastName properties from an object that contains more than these properties, set the Select property to new(FirstName, LastName). The LinqDataSource control will dynamically create the new class that you specified in the new method.

You can use the As keyword to assign an alias to a selected value. If you assign an alias, you must include the aliased properties in the new method. Any data-bound control that binds to the results from the LinqDataSource control must refer to the property by the alias. For example, if you set the Select property to new(Region As SalesRegion, Revenue / 1000 As RevenueThousands), the data-bound control can access two properties, SalesRegion and RevenueThousands.

You use the new function in the Select property according to the conditions listed in the following table.

Select Use of new Example
Single property. new not needed. Select="Category"
Multiple properties. new needed. Select="new(Category, Name)"
Single or multiple properties that have an alias. new needed. Select="new(Category As CategoryGroup)"

If you want to enable automatic data modifications, but you do not want to display all the properties from the data object, do not set the Select property. Instead, manage the display through the data-bound control. When you use the DetailsView control or the GridView control, you must also set the AutoGenerateRows or the AutoGenerateColumns property to false. Any values that are not displayed in the data-bound control are stored in view state. They are passed unchanged to the data source when the data update is performed.

When you group data in the query, you can include aggregate methods in the Select property. You can use the following aggregate methods:

  • Count()

  • Average( column )

  • Sum( column )

  • Max( column )

  • Min( column )

  • Where( condition )

  • Any()

  • All( condition )

For more information, see LinqDataSource Web Server Control Overview and How to: Group and Aggregate Data Using the LinqDataSource Control.

When you group data, you can include Key and It, which are dynamically created properties, in the Select property. The Key property refers to the values in the property or properties that were used to group the data. For example, if you group by a property named Category, the Key property will contain each unique value in the Category property. The It property refers to a collection of individual records in a grouping of data. You can iterate over the It property to retrieve the individual records that have been consolidated in the grouping operation. For example, if you group by a property named Category, the It property contains all the individual records that share a common value in the Category property.

If you include the It keyword in the Select property when you group data, you must assign an alias to the It keyword. The LinqDataSource control will throw an exception if It does not have an alias. You can assign an alias to the Key keyword, but this is not required.

Applies to