建立 DataView 物件 (LINQ to DataSet)Creating a DataView Object (LINQ to DataSet)

有兩種方式可以DataView在 LINQ to DataSet 內容中建立。There are two ways to create a DataView in the LINQ to DataSet context. 您可以DataViewDataTable上的 LINQ to DataSet 查詢建立,或是從具類型或不具類型DataTable的來建立。You can create a DataView from a LINQ to DataSet query over a DataTable, or you can create it from a typed or un-typed DataTable. 在這兩種情況下, DataView您可以使用其中一個AsDataView擴充方法來建立:DataView不會直接在 LINQ to DataSet 內容中可建構。In both cases, you create the DataView by using one of the AsDataView extension methods; DataView is not directly constructible in the LINQ to DataSet context.

在您已經建立 DataView 之後,就可以將它繫結程序至 Windows Forms 應用程式或 ASP.NET 應用程式中的 UI 控制項,也可以變更篩選和排序設定。After the DataView has been created, you can bind it to a UI control in a Windows forms application or an ASP.NET application, or change the filtering and sorting settings.

DataView 會建構索引,以便大幅增加可使用索引之作業的效能,例如篩選和排序。DataView constructs an index, which significantly increases the performance of operations that can use the index, such as filtering and sorting. 在您建立 DataView 以及修改任何排序或篩選資訊時,系統就會建立 DataView 的索引。The index for a DataView is built both when the DataView is created and when any of the sorting or filtering information is modified. 如果您建立 DataView,然後設定排序或篩選資訊,將會導致系統至少建立索引兩次:一次是建立 DataView 時,另一次是修改任何排序或篩選屬性時。Creating a DataView and then setting the sorting or filtering information later causes the index to be built at least twice: once when the DataView is created, and again when any of the sort or filter properties are modified.

如需使用DataView進行篩選和排序的詳細資訊,請參閱使用 dataview 進行篩選使用 dataview 進行排序For more information about filtering and sorting with DataView, see Filtering with DataView and Sorting with DataView.

從 LINQ to DataSet 查詢中建立 DataViewCreating DataView from a LINQ to DataSet Query

您可以從 LINQ to DataSet 查詢的結果建立DataRow 物件,其中的結果是物件的投影。DataViewA DataView object can be created from the results of a LINQ to DataSet query, where the results are a projection of DataRow objects. 新建立的 DataView 會從建立此物件的查詢中繼承篩選和排序資訊。The newly created DataView inherits the filtering and sorting information from the query it is created from.

注意

在大部分清況中,用於篩選和排序的運算式不應該具有副作用 (Side Effect) 而且必須具決定性。In most cases, the expressions used for filtering and sorting should not have side effects and must be deterministic. 此外,這些運算式不應該包含取決於固定執行次數的任何邏輯,因為排序和篩選作業可能會執行任何次數。Also, the expressions should not contain any logic that depend on a set number of executions, as the sorting and filtering operations may be executed any number of times.

目前不支援從傳回匿名型別的查詢或執行聯結 (Join) 作業的查詢中建立 DataViewCreating a DataView from a query that returns anonymous types or queries that perform join operations is not supported.

在用來建立 DataView 的查詢中只支援使用下列查詢運算子:Only the following query operators are supported in a query used to create DataView:

請注意,當DataView從 LINQ to DataSet 查詢建立時, Select方法必須是在查詢中呼叫的最後方法。Note that when a DataView is created from a LINQ to DataSet query the Select method must be the final method called in the query. 下列範例會顯示這種情況,它會建立DataView依總到期日排序的線上訂單:This is shown in the following example, which creates a DataView of online orders sorted by total due:

DataTable orders = dataSet.Tables["SalesOrderHeader"];

EnumerableRowCollection<DataRow> query =
    from order in orders.AsEnumerable()
    where order.Field<bool>("OnlineOrderFlag") == true
    orderby order.Field<decimal>("TotalDue")
    select order;

DataView view = query.AsDataView();

bindingSource1.DataSource = view;
Dim orders As DataTable = dataSet.Tables("SalesOrderHeader")

Dim query = _
    From order In orders.AsEnumerable() _
    Where order.Field(Of Boolean)("OnlineOrderFlag") = True _
    Order By order.Field(Of Decimal)("TotalDue") _
    Select order

Dim view As DataView = query.AsDataView()
bindingSource1.DataSource = view

從查詢建立RowFilter DataView之後,您也可以使用Sort以字串為基礎的和屬性來篩選和排序。You can also use the string-based RowFilter and Sort properties to filter and sort a DataView after it has been created from a query. 請注意,這樣做會清除繼承自查詢的排序和篩選資訊。Note that this will clear the sorting and filtering information inherited from the query. 下列範例會根據以DataView ' s ' 開頭的姓氏進行篩選的 LINQ to DataSet 查詢來建立。The following example creates a DataView from a LINQ to DataSet query that filters by last names that start with 'S'. 以字串為基礎的 Sort 屬性會設定為按照遞增順序排序姓氏,然後再按照遞減順序排序名字:The string-based Sort property is set to sort on last names in ascending order and then first names in descending order:

DataTable contacts = dataSet.Tables["Contact"];

EnumerableRowCollection<DataRow> query = from contact in contacts.AsEnumerable()
                                         where contact.Field<string>("LastName").StartsWith("S")
                                         select contact;

DataView view = query.AsDataView();

bindingSource1.DataSource = view;

view.Sort = "LastName desc, FirstName asc";
Dim contacts As DataTable = dataSet.Tables("Contact")

Dim query = _
    From contact In contacts.AsEnumerable() _
    Where contact.Field(Of String)("LastName").StartsWith("S") _
    Select contact

Dim view As DataView = query.AsDataView()
bindingSource1.DataSource = view
view.Sort = "LastName desc, FirstName asc"

從 DataTable 中建立 DataViewCreating a DataView from a DataTable

除了從 LINQ to DataSet 查詢建立之外,也可以使用DataView AsDataView方法, DataTable從建立物件。In addition to being created from a LINQ to DataSet query, a DataView object can be created from a DataTable by using the AsDataView method.

下列範例會從 SalesOrderDetail 資料表中建立 DataView,然後將它設定為 BindingSource 物件的資料來源。The following example creates a DataView from the SalesOrderDetail table and sets it as the data source of a BindingSource object. 這個物件會當做 DataGridView 控制項的 Proxy。This object acts as a proxy for a DataGridView control.

DataTable orders = dataSet.Tables["SalesOrderDetail"];

DataView view = orders.AsDataView();
bindingSource1.DataSource = view;

dataGridView1.AutoResizeColumns();
Dim orders As DataTable = dataSet.Tables("SalesOrderDetail")

Dim view As DataView = orders.AsDataView()
bindingSource1.DataSource = view
dataGridView1.AutoResizeColumns()

在您已經從 DataView 中建立 DataTable 之後,就可以針對它設定篩選和排序。Filtering and sorting can be set on the DataView after it has been created from a DataTable. 下列範例會從 Contact 資料表中建立 DataView 並將 Sort 屬性設定為按照遞增順序排序姓氏,然後再按照遞減順序排序名字:The following example creates a DataView from the Contact table and sets the Sort property to sort on last names in ascending order and then first names in descending order:

DataTable contacts = dataSet.Tables["Contact"];

DataView view = contacts.AsDataView();

view.Sort = "LastName desc, FirstName asc";

bindingSource1.DataSource = view;
dataGridView1.AutoResizeColumns();
Dim contacts As DataTable = dataSet.Tables("Contact")

Dim view As DataView = contacts.AsDataView()

view.Sort = "LastName desc, FirstName asc"

bindingSource1.DataSource = view
dataGridView1.AutoResizeColumns()

不過,在您已經從查詢中建立 RowFilter 之後設定 SortDataView 屬性會發生效能降低的情況,因為 DataView 會建構索引來支援篩選和排序作業。However, there is a performance loss that comes with setting the RowFilter or Sort property after the DataView has been created from a query, because DataView constructs an index to support filtering and sorting operations. 設定 RowFilterSort 屬性會重建資料索引,因而增加應用程式的負荷並降低效能。Setting the RowFilter or Sort property rebuilds the index for the data, adding overhead to your application and decreasing performance. 如果可能的話,最好是在您首次建立 DataView 時指定篩選和排序資訊,並且避免之後修改這項資訊。When possible, it is better to specify the filtering and sorting information when you first create the DataView and avoid modifying it afterwards.

另請參閱See also