Export (0) Print
Expand All

LinqDataSourceSelectEventArgs.Result Property

Gets or sets the data object that is used in the data query.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web.Extensions (in System.Web.Extensions.dll)

'Declaration
Public Property Result As Object

Property Value

Type: System.Object
An object that represents the data for the query.

By default, the LinqDataSource control applies its query expressions to the object defined in the TableName property. In a handler for the Selecting event, you can manually change which object is queried by setting the Result property to an object. For example, you can use the Result property to query an in-memory collection in the Web page, or to get the results from a LINQ query expression. You can set the Result property to any object. If the object does not implement the IEnumerable(Of T) interface, the LinqDataSource control wraps the object in an object that does implement the IEnumerable(Of T) interface.

When the Result property is set to any value other than Nothing, the LinqDataSource control does not query the object defined in the TableName property. Instead, it queries the object in the Result property.

NoteNote

When you set the Result property to an object, do not use Nothing to represent an object that does not contain any data. The LinqDataSource control interprets Nothing to mean that the Result property is not set, and it will create and query the object in the TableName property. To represent an object that does not contain data, set the Result property to an IList or IList(Of T) object that does not contain any elements.

The ContextCreating, ContextCreated, and ContextDisposing events are not raised when you programmatically set the Result property to an object, and when two additional conditions apply. The conditions are that either the original values do not have to be stored in view state, or the object in the Result property implements the ITable interface.

The following example shows how to set the Result property to the result of a LINQ query.

Protected Sub LinqDataSource_Selecting(sender As Object, e As LinqDataSourceSelectEventArgs)
    Dim exampleContext As New ExampleDataContext()

    e.Result = From p In exampleContext.Products Where p.Category = "Beverages" 
               Select New With { _
                    Key .ID = p.ProductID, _
                    Key .Name = p.Name _
    }
End Sub

The following example shows how to set the Result property to an array of string values is defined in the Web page.

Partial Class Default3
    Inherits System.Web.UI.Page

    Dim citiesArray() As String = _
    { _
        "Atlanta", _
        "Charlotte", _
        "Denver", _
        "New York", _
        "San Francisco" _
    }


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub 


    Protected Sub LinqDataSource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
        Dim cities = From city In citiesArray _
                     Where city > "B" _
                     Select city
        e.Result = cities
        ' Or we could set e.Result = citiesArray to return all rows. 
    End Sub 

End Class

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5
Show:
© 2015 Microsoft