DataView.RowFilter 与 DataTable.Select() 与 DataTable.Rows.Find() [英] DataView.RowFilter Vs DataTable.Select() vs DataTable.Rows.Find()

查看:44
本文介绍了DataView.RowFilter 与 DataTable.Select() 与 DataTable.Rows.Find()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的代码:

Dataview someView = new DataView(sometable)
someView.RowFilter = someFilter;

if(someView.count > 0) {  …. }

很多文章都说 Datatable.Select() 比使用 DataViews 更好,但这些都是在 VS2008 之前的.

Quite a number of articles which say Datatable.Select() is better than using DataViews, but these are prior to VS2008.

解决了:DataView 大记录集性能不佳之谜
数据记录数组与数据视图:性能的巨大差异

谷歌搜索这个主题,我发现一些文章/论坛主题提到 Datatable.Select() 本身是相当错误的(不确定)并且在各种情况下表现不佳.

Googling on this topic I found some articles/forum topics which mention Datatable.Select() itself is quite buggy(not sure on this) and underperforms in various scenarios.

关于 msdn 上的这个(Best Practices ADO.NET)主题建议如果在数据表上定义了主键,则应使用 findrows() 或 find() 方法插入 Datatable.Select().

On this(Best Practices ADO.NET) topic on msdn it is suggested that if there is primary key defined on a datatable the findrows() or find() methods should be used insted of Datatable.Select().

这篇文章这里 (.NET 1.1) 对这三者进行了基准测试方法加上几个.但这是针对 1.1 版的,所以不确定这些现在是否仍然有效.根据此 DataRowCollection.Find() 优于所有方法,Datatable.Select() 优于 DataView.RowFilter.

This article here (.NET 1.1) benchmarks all the three approaches plus a couple more. But this is for version 1.1 so not sure if these are valid still now. Accroding to this DataRowCollection.Find() outperforms all approaches and Datatable.Select() outperforms DataView.RowFilter.

所以我很困惑什么可能是在数据表中查找行的最佳方法.或者没有单一的好方法可以做到这一点,根据场景存在多种解决方案?

So I am quite confused on what might be the best approach on finding rows in a datatable. Or there is no single good way to do this, multiple solutions exist depending upon the scenario?

推荐答案

您正在寻找在数据表中查找行的最佳方法",所以我首先要问:最佳"用于什么?我认为,任何技术都有可能比其他技术更适合的场景.

You are looking for the "best approach on finding rows in a datatable", so I first have to ask: "best" for what? I think, any technique has scenarios where it might fit better then the others.

首先,让我们看看DataView.RowFilter:DataView 在数据绑定方面有一些优势.它非常面向视图,因此具有强大的排序、过滤或搜索功能,但会产生一些开销并且未针对性能进行优化.我会选择 DataView.RowFilter 用于较小的记录集和/或您利用其他功能的地方(例如,对视图的直接数据绑定).

First, let's look at DataView.RowFilter: A DataView has some advantages in Data Binding. Its very view-oriented so it has powerful sorting, filtering or searching features, but creates some overhead and is not optimized for performance. I would choose the DataView.RowFilter for smaller recordsets and/or where you take advantage of the other features (like, a direct data binding to the view).

有关 DataView 的大多数事实(您可以在较早的帖子中阅读)仍然适用.

Most facts about the DataView, which you can read in older posts, still apply.

其次,如果您只想点击一次,您应该更喜欢 DataTable.Rows.Find 而不是 DataTable.Select.为什么?DataTable.Rows.Find 仅返回单行.本质上,当您指定主键时,就会创建一个二叉树.这有一些与之相关的开销,但极大地加快了检索速度.

Second, you should prefer DataTable.Rows.Find over DataTable.Select if you want just a single hit. Why? DataTable.Rows.Find returns only a single row. Essentially, when you specify the primary key, a binary tree is created. This has some overhead associated with it, but tremendously speeds up the retrieval.

DataTable.Select 速度较慢,但​​如果您有多个条件并且不关心索引或未索引的行,它会非常方便:它基本上可以找到所有内容,但未针对性能进行优化.本质上,DataTable.Select 必须遍历整个表并将每条记录与您传入的条件进行比较.

DataTable.Select is slower, but can come very handy if you have multiple criteria and don't care about indexed or unindexed rows: It can find basically everything but is not optimized for performance. Essentially, DataTable.Select has to walk the entire table and compare every record to the criteria that you passed in.

希望这篇小概述对您有所帮助.

I hope you find this little overview helpful.

我建议看看这篇文章,关于性能问题对我很有帮助.这篇文章包含了一些引述.

I'd suggest to take a look at this article, it was helpful for me regarding performance questions. This post contains some quotes from it.

一点更新:顺便说一句,这似乎有点超出您的问题范围,但它几乎总是在后端进行过滤和搜索的最快解决方案.如果您想要简单性并拥有 SQL Server 作为后端和 .NET3+ 在客户端,请选择 LINQ-to-SQL.搜索 Linq 对象非常方便,并且可以创建在服务器端执行的查询.虽然 LINQ-to-Objects 也是一种非常舒适但速度较慢的技术.如果你还不知道......

A little UPDATE: By the way, this might seem a little out of scope of your question, but its nearly always the fastest solution to do the filtering and searching on the backend. If you want the simplicity and have an SQL Server as backend and .NET3+ on client, go for LINQ-to-SQL. Searching Linq objects is very comfortable and creates queries which are performed on server side. While LINQ-to-Objects is also a very comfortable but also slower technique. In case you didn't know already....

这篇关于DataView.RowFilter 与 DataTable.Select() 与 DataTable.Rows.Find()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆