数据表过滤:linq 与过滤器? [英] Datatable filtering: linq vs filter?

查看:23
本文介绍了数据表过滤:linq 与过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过滤内存对象(数据表):

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.SelectEnumerable.Where 需要更多的内存,因为后者只是DataTableDataRowCollection 上的循环,而旧的 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屋!

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