LinqDataSourceSelectEventArgs.Result 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定用於資料查詢的資料物件。
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 物件。
Note
當你將屬性設定 Result 為物件時,請不要用來 null 表示一個不包含任何資料的物件。
LinqDataSource控制項解釋null為Result該屬性未被設定,並會建立並查詢該屬性中的TableName物件。 要表示一個不包含資料的物件,將屬性ResultIList設為一個不包含任何元素的物件IList<T>。
ContextCreating當你程式化地將屬性設定ContextCreated為物件,且有兩個額外條件時,這些 、 ContextDisposing和 Result 事件並不會被觸發。 條件是原始值不必以視圖狀態儲存,或是屬性中的 Result 物件實作介面 ITable 。