DataView 性能

本主题讨论使用 DataView 类的 FindFindRows 方法并在 Web 应用程序中缓存 DataView 时所具有的性能优势。

Find 和 FindRows

DataView 构造一个索引。 索引包含从表或视图中的一个或多个列生成的键。 这些键存储在结构中,这种结构可使 DataView 能够快速有效地查找行或与键值关联的行。 使用索引的操作(如筛选和排序)可显著提高性能。 创建 DataView 及修改任何排序或筛选信息时,均会生成 DataView 的索引。 创建 DataView 然后设置排序或筛选信息会使索引生成至少两次:一次是在创建 DataView 时,另一次是在修改任何排序或筛选属性时。 有关使用 DataView 进行筛选和排序的更多信息,请参见使用 DataView 进行筛选 (LINQ to DataSet)使用 DataView 进行排序 (LINQ to DataSet)

如果要返回特定数据查询的结果而不是提供数据子集的动态视图,则可以使用 DataViewFindFindRows 方法,而不设置 RowFilter 属性。 RowFilter 属性最适合用于用绑定控件显示筛选结果的数据绑定应用程序。 设置 RowFilter 属性会重新生成数据的索引,从而增加应用程序的系统开销并降低性能。 FindFindRows 方法使用当前索引,而不要求重新生成索引。 如果只想调用 FindFindRows 一次,则应使用现有的 DataView。 如果想要调用 FindFindRows 多次,则应该创建一个新的 DataView 以便对想要搜索的列重新生成索引,然后调用 FindFindRows 方法。 有关 FindFindRows 方法的更多信息,请参见查找行 (ADO.NET)

下面的示例使用 Find 方法来查找姓氏为“Zhu”的联系人。

Dim contacts As DataTable = dataSet.Tables("Contact")

Dim query = _
    From contact In contacts.AsEnumerable() _
    Order By contact.Field(Of String)("LastName") _
    Select contact

Dim view As DataView = query.AsDataView()

Dim found As Integer = view.Find("Zhu")

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

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

DataView view = query.AsDataView();

// Find a contact with the last name of Zhu.
int found = view.Find("Zhu");

下面的示例使用 FindRows 方法来查找所有红颜色的产品。

Dim products As DataTable = dataSet.Tables("Product")

Dim query = _
From product In products.AsEnumerable() _
Order By product.Field(Of Decimal)("ListPrice"), product.Field(Of String)("Color") _
Select product

Dim view As DataView = query.AsDataView()
view.Sort = "Color"

Dim criteria As Object() = New Object() {"Red"}

Dim foundRowsView As DataRowView() = view.FindRows(criteria)
DataTable products = dataSet.Tables["Product"];

EnumerableRowCollection<DataRow> query = from product in products.AsEnumerable()
                                         orderby product.Field<Decimal>("ListPrice"), product.Field<string>("Color") 
                                         select product;

DataView view = query.AsDataView();

view.Sort = "Color";

object[] criteria = new object[] { "Red"};

DataRowView[] foundRowsView = view.FindRows(criteria);            

ASP.NET

ASP.NET 具有一种缓存机制,允许您在内存中存储需要创建大量服务器资源的对象。 缓存这些类型的资源可以显著提高应用程序的性能。 缓存由 Cache 类实现,缓存实例专用于每个应用程序。 由于创建新的 DataView 对象需要大量资源,因此您可能希望在 Web 应用程序中使用此缓存功能,使得每次刷新网页时,不必重新生成 DataView

在下面的示例中,对 DataView 进行缓存以便在刷新该页时不必对数据重新排序。

If (Cache("ordersView") = Nothing) Then
                
Dim dataSet As New DataSet()
            
   FillDataSet(dataSet)
                
   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()
                
   Cache.Insert("ordersView", view)
                
End If
            
Dim ordersView = CType(Cache("ordersView"), DataView)

GridView1.DataSource = ordersView
GridView1.DataBind()
if (Cache["ordersView"] == null)
{
   // Fill the DataSet.                
   DataSet dataSet = FillDataSet();

   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();
   Cache.Insert("ordersView", view);
}

DataView ordersView = (DataView)Cache["ordersView"];

GridView1.DataSource = ordersView;
GridView1.DataBind();

请参见

概念

数据绑定和 LINQ to DataSet