Bagikan melalui


LinqDataSourceSelectEventArgs.Result Properti

Definisi

Mendapatkan atau mengatur objek data yang digunakan dalam kueri data.

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

Nilai Properti

Objek yang mewakili data untuk kueri.

Contoh

Contoh berikut menunjukkan cara mengatur Result properti ke hasil kueri 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

Contoh berikut menunjukkan cara mengatur Result properti ke array nilai string ditentukan di halaman Web.

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

Keterangan

Secara default, LinqDataSource kontrol menerapkan ekspresi kuerinya ke objek yang ditentukan dalam TableName properti . Dalam handler untuk Selecting acara, Anda dapat mengubah objek mana yang dikueri secara manual dengan mengatur Result properti ke objek. Misalnya, Anda dapat menggunakan Result properti untuk mengkueri koleksi dalam memori di halaman Web, atau untuk mendapatkan hasil dari ekspresi kueri LINQ. Anda dapat mengatur Result properti ke objek apa pun. Jika objek tidak mengimplementasikan IEnumerable<T> antarmuka, LinqDataSource kontrol membungkus objek dalam objek yang mengimplementasikan IEnumerable<T> antarmuka.

Result Saat properti diatur ke nilai apa pun selain null, LinqDataSource kontrol tidak mengkueri objek yang ditentukan dalam TableName properti . Sebaliknya, ia meminta objek di Result properti .

Catatan

Saat Anda mengatur Result properti ke objek, jangan gunakan null untuk mewakili objek yang tidak berisi data apa pun. LinqDataSource Kontrol menginterpretasikan null berarti bahwa Result properti tidak diatur, dan akan membuat dan mengkueri objek dalam TableName properti . Untuk mewakili objek yang tidak berisi data, atur Result properti ke IList objek atau IList<T> yang tidak berisi elemen apa pun.

Peristiwa ContextCreating, ContextCreated, dan ContextDisposing tidak dimunculkan ketika Anda secara terprogram mengatur Result properti ke objek, dan ketika dua kondisi tambahan berlaku. Kondisinya adalah bahwa nilai asli tidak harus disimpan dalam status tampilan, atau objek dalam Result properti mengimplementasikan ITable antarmuka.

Berlaku untuk

Lihat juga