问题描述
过滤内存对象(数据):
filtering an memory object ( datatble) :
在执行之间有很大的不同:
Is there huge different between doing :
var t = dt.Select("id=2");
vs
var g = dt.AsEnumerable().Where(f => f["id"].ToString() == "2");
推荐答案
我假设 DataTable。选择
需要比 Enumerable.Where
更多的内存,因为后者只是 DataRowCollection $ c $
DataTable
,而旧的 DataTable.Select
创建新对象,如选择
或
DataExpression
,甚至从查询返回新对象( DataRow []
)。 Enumerable.Where
只是在循环中使用谓词来确定要返回的内容。它也被执行懒惰,所以你可以从结果中说10行。
I assume that DataTable.Select
needs even more memory than Enumerable.Where
since the latter is just a loop on the DataRowCollection
of the DataTable
whereas the old DataTable.Select
creates new objects like Select
or DataExpression
and it even returns new objects(DataRow[]
) from the query. Enumerable.Where
just uses a predicate in a loop to determine what to return. It's also executed lazily, so you could just take, say 10 rows from the result.
var rows = dt.AsEnumerable()
.Where(row => row.Field<int>("id") == 2)
.Take(10); // not possible with DataTable.Select
两者都是内存中的查询,所以没有什么不同。
Both are in-memory queries, so there's not a great difference.
我会选择更易读,更强大,更强大和更强大的类型(扩展名): Linq-To-DataTable
。
I would chose what is more readable, powerful and maintanable and also strongly typed(Field
extensions): Linq-To-DataTable
.
这篇关于可数据过滤:linq vs过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!