数据表选择与 LINQ 选择 [英] DataTable Select vs LINQ Select
问题描述
关于何时 DataTable.Select 的任何建议 应该与 LINQ Select 在处理内存数据表时?
Any advice on when DataTable.Select should be used versus LINQ Select when dealing with an in-memory DataTable?
我发现 LINQ 语法更简单、更强大,但我不确定是否存在性能或其他问题使 DataTable select 更可取.
I find LINQ syntax easier and more powerful, but I'm not sure if there are performance or other issues which make a DataTable select preferable.
(我使用的第三方 API 提供了一个从数据库预先填充的 DataTable.我需要在内存中进一步过滤.)
(I'm using a third party API that provides a DataTable that has been pre-populated from the database. I need to filter that further in-memory.)
推荐答案
根据个人经验,我尽量避免使用 Datatable.Select.我发现它很慢并且有一些奇怪的错误.
Based upon personal experience, I try to avoid the Datatable.Select. I find it to be slow and has some odd bugs.
我遇到的一个(由 Microsoft 确认和记录)错误是,当语句中有括号时,DataTable.Select 并不总是正确评估 AND 条件.
One (confirmed and documented by Microsoft) bug I ran into was that DataTable.Select doesn't always evaluate AND conditions correctly when there are parenthesis in the statement.
例如,(Col1 > 1) AND (Col <10) 可能无法返回正确答案,而 Col1 > 1 AND Col <10 将正常工作.
For example, (Col1 > 1) AND (Col < 10) can fail to return correct answers, whereas Col1 > 1 AND Col < 10 will work correctly.
此错误不会出现在每台计算机上.就我而言,我使用的检查在我的开发平台和除一台客户端计算机之外的每台客户端计算机上都运行良好.在我发现这个错误后,我开始转向使用 LINQ 进行选择,并注意到操作速度显着提高.
This bug doesn't show up on every computer. In my case the check I was using ran fine on my development platform and every client computer except one. After I discovered this bug I began shifting to using LINQ for selects and noticed a significant increase in the speed of the operations.
旁注:无需详细解释,我的公司不使用数据库来存储数据.所有我们对数据表的操作都涉及从平面文件加载的内存表.所以我不是在谈论 LINQ 2 SQL,而是在谈论 LINQ to Dataset.
Side note: Without going into long explanations, my company doesn't use a database to store data. All of our operations with DataTables involve in memory tables loaded from flat-files. So I am not talking about LINQ 2 SQL, but LINQ to Dataset.
这篇关于数据表选择与 LINQ 选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!