本文介绍了使用哪一个; Datatable.Select()或LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个数据表有或大或小的数据,从数据表中选择数据,我可以使用 Datatable.Select( )方法或去LINQ。哪一个是速度更快,效率呢?

If I have a DataTable with a small or large amount of data, to select data from the datatable I can use the Datatable.Select() method or go for LINQ. Which one is faster and efficient?

推荐答案

哪一个是最适合你的情况?或者,更重要的是,确实为你查询使它值得选择一个比其他的数据量的速度差?

Which one is best for your circumstance? Or, more importantly, does the speed difference for the amount of data you're querying make it worthwhile to choose one over the other?

LINQ通常更易于阅读,在我看来,比数据过滤的几乎任何其他形式的优点超过使用之中,至少部分地,强类型使其更难让 DataTable.Select 错误。

LINQ is generally easier to read, on my opinion, than pretty much any other form of data filtering and has the advantage over using DataTable.Select of being, at least partially, strongly-typed making it harder to make mistakes.

// Using DataTable.Select()
DataRow[] records = myTable.Select("(MyIntField > 30 AND MyStringField == 'StringValue') OR AnotherField > 70");

// Using LINQ
var records = from record in myTable.AsEnumerable()
              where (record.Field<int>("MyIntField") > 30
                     && Record.Field<string>("StringValue") == "StringValue")
                    ||
                    (record.Field<int>("AnotherField") > 70)
              select record;



LINQ查询比较大,但我个人认为这是更具可读性。

The LINQ query is bigger, but personally I think it's more readable

这篇关于使用哪一个; Datatable.Select()或LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 00:16