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)

public Object Result { get; set; }

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<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.

NoteNote

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.

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
         };
}

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.
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft