数据表过滤:LINQ VS过滤器?过滤器、数据表、LINQ、VS

由网友(?妇科妙手)分享简介:筛选的内存对象(datatble):在做之间有巨大的不同:变种T = dt.Select(ID = 2);VS 变种G = dt.AsEnumerable()式(F => F [ID]的ToString()==2)。解决方案 我认为 DataTable.Select 比 Enumerable.Where需要更多的...

筛选的内存对象(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.

阅读全文

相关推荐

最新文章