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 对象。
注释
将属性设置为 Result 对象时,请勿用于 null 表示不包含任何数据的对象。 该 LinqDataSource 控件解释 null 为表示 Result 未设置属性,它将在属性中创建 TableName 和查询对象。 若要表示不包含数据的对象,请将 Result 属性设置为 IList 不包含任何元素的或 IList<T> 对象。
当以编程方式将Result属性设置为对象时,以及应用两个附加条件时,不会引发该ContextCreating属性ContextCreated和ContextDisposing事件。 条件是,原始值不必存储在视图状态,或者属性中的 Result 对象实现 ITable 接口。