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

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

问题描述

筛选的内存对象(datatble):

在做之间有巨大的不同:

 变种T = dt.Select(ID = 2);
 

VS

 变种G = dt.AsEnumerable()式(F => F [ID]的ToString()==2)。
 

解决方案

我认为 DataTable.Select Enumerable.Where需要更多的内存,因为后者只是对 DataRowCollection 数据表,而旧的一个循环 DataTable.Select 创建像选择新的对象 DataEx pression ,它甚至返回从查询新对象(的DataRow [] )。 Enumerable.Where 只需使用predicate在一个循环来决定返回什么。它也懒洋洋地执行,所以你可以只取,说从结果10行。

  VAR行= dt.AsEnumerable()
             。凡(行=> row.Field< INT>(ID)== 2)
             。取(10); //不可能的DataTable.Select
 

无论是内存中的查询,所以没有很大的区别。

我会选择什么是更具可读性,功能强大,maintanable也强类型( 字段 扩展): LINQ到数据表

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");

解决方案

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.

I would chose what is more readable, powerful and maintanable and also strongly typed(Field extensions): Linq-To-DataTable.

这篇关于数据表过滤:LINQ VS过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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