数据表过滤:linq 与过滤器? [英] Datatable filtering: linq vs filter?
问题描述
过滤内存对象(数据表):
filtering an memory object ( datatble) :
这样做是否有很大的不同:
Is there huge different between doing :
var t = dt.Select("id=2");
对比
var g = dt.AsEnumerable().Where(f => f["id"].ToString() == "2");
推荐答案
我认为 DataTable.Select
比 Enumerable.Where
需要更多的内存,因为后者只是DataTable
的 DataRowCollection
上的循环,而旧的 DataTable.Select
创建新对象,如 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.
我会选择可读性更好、功能更强大、更易于维护并且也是强类型的(Field
扩展):Linq-To-DataTable
.
I would chose what is more readable, powerful and maintanable and also strongly typed(Field
extensions): Linq-To-DataTable
.
这篇关于数据表过滤:linq 与过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!