Bewerken

Delen via


LinqDataSourceSelectEventArgs.Result Property

Definition

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

public:
 property System::Object ^ Result { System::Object ^ get(); void set(System::Object ^ value); };
public object Result { get; set; }
member this.Result : obj with get, set
Public Property Result As Object

Property Value

An object that represents the data for the query.

Examples

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

protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    ExampleDataContext exampleContext = new ExampleDataContext();
    
    e.Result = from p in exampleContext.Products 
         where p.Category == "Beverages"
         select new {
           ID = p.ProductID,
           Name = p.Name
         };
}
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.

public partial class Default3 : System.Web.UI.Page
{
    string[] citiesArray = 
    { 
        "Atlanta", 
        "Charlotte", 
        "Denver", 
        "New York", 
        "San Francisco" 
    };

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void LinqDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        var cities = from city in citiesArray
                     where city.CompareTo("B") > 0
                     select city;
        e.Result = cities;
        // Or we could set e.Result = citiesArray to return all rows.
    }
}
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

Remarks

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<T> interface, the LinqDataSource control wraps the object in an object that does implement the IEnumerable<T> interface.

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

Note

When you set the Result property to an object, do not use null to represent an object that does not contain any data. The LinqDataSource control interprets null 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<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.

Applies to

See also