LinqDataSourceSelectEventArgs.Result 屬性

定義

取得或設定資料查詢中使用的資料物件。

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

屬性值

物件,表示查詢的資料。

範例

下列範例示範如何將 屬性設定 Result 為 LINQ 查詢的結果。

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

下列範例示範如何將 屬性設定 Result 為字串值的陣列定義于網頁中。

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

備註

根據預設, LinqDataSource 控制項會將其查詢運算式套用至 屬性中 TableName 定義的 物件。 在 事件的處理常式中 Selecting ,您可以將 屬性設定 Result 為 物件,以手動方式變更要查詢的物件。 例如,您可以使用 Result 屬性來查詢網頁中的記憶體內部集合,或從 LINQ 查詢運算式取得結果。 您可以將 屬性設定 Result 為任何物件。 如果物件未實作 IEnumerable<T> 介面, LinqDataSource 控制項會將 物件包裝在實作 介面的物件 IEnumerable<T> 中。

Result當 屬性設定為 以外的 null 任何值時, LinqDataSource 控制項不會查詢 屬性中 TableName 定義的物件。 相反地,它會查詢 屬性中的 Result 物件。

注意

當您將 Result 屬性設定為 物件時,請勿使用 null 來表示不包含任何資料的 物件。 控制項 LinqDataSource 會解譯 null 為表示 Result 屬性未設定,而且它會在 屬性中 TableName 建立及查詢物件。 若要表示不包含資料的 物件,請將 Result 屬性設定為 IList 不包含任何專案的 或 IList<T> 物件。

ContextCreating當您以程式設計方式將 Result 屬性設定為 物件,以及套用兩個額外的條件時,不會引發 、 ContextCreatedContextDisposing 事件。 條件是原始值不需要儲存在檢視狀態,或 屬性中的 Result 物件會實作 ITable 介面。

適用於

另請參閱