在ADO.NET中对数据表进行排序的最佳方法是什么?

时间:2021-11-12 21:18:23

What is the best for sorting a data table in c#, both from a performance and a code-readability point-of-view:

从性能和代码可读性的角度来看,在c#中对数据表进行排序的最佳方法是什么:

personsDT.OrderBy(person => person.PersonName);

or:

personsDT.DefaultView.Sort = "PersonName ASC";

The personsDT is build from a SharePoint list, so it is impossible to use SQL (I am aware that an ORDER BY claude in a SQL SELECT statement would be the best way). Considering the performance, I am worried that the OrderBy<> clause might be slower than the Sort in the data view. Are you aware of such performance implications?

personsDT是从SharePoint列表构建的,因此不可能使用SQL(我知道SQL SELECT语句中的ORDER BY claude是最好的方法)。考虑到性能,我担心OrderBy <>子句可能比数据视图中的Sort慢。您是否意识到这种性能影响?

4 个解决方案

#1


I'll prefer the first option.

我更喜欢第一种选择。

1) For code the readability point-of-view i think that Lambda is clearer than the second one.

1)对于代码,可读性的观点我认为Lambda比第二个更清晰。

2) In the first case you are using a strongly type way to sort your entity which is good.

2)在第一种情况下,您使用强类型方式对您的实体进行排序,这是好的。

3) On the second case you are passing the field and the order in a string mmmm dont like it.

3)在第二种情况下,你传递的字段和字符串mmmm不喜欢它。

Go Lambdas!!!

Regards!

#2


What's the purpose for the question? Performance is almost certainly a non-issue; if not, you'll need to test it in your context. I think if you are specifying a sort for a SQL statement it should be in the SQL statement; and I prefer to avoid constructing SQL statements stringwise if possible. Which do you like better esthetically?

这个问题的目的是什么?表现几乎肯定不是问题;如果没有,你需要在你的上下文中测试它。我想如果你为SQL语句指定一个排序,它应该在SQL语句中;如果可能的话,我更愿意避免以字符串方式构造SQL语句。你更喜欢哪种美学?

#3


To throw my 2c into the pot:

把我的2c扔进锅里:

Coming from an sql background, I personally find the sql way more intuitive - but that's no reason to follow it. The lambda method has several good recommendations for it:

来自sql背景,我个人觉得sql方式更直观 - 但是没有理由遵循它。 lambda方法有几个很好的建议:

As MRFerocius (is the missing 'o' intentional?) points out - the lambda method is strongly typed. That's a good thing.

正如MRFerocius(缺少'o'故意?)指出的那样 - lambda方法是强类型的。这是好事。

And then there's this - if your sorting criteria should ever become more complicated, the lambda method would allow you to tack on additional and esoteric conditions that the sql method would not.

然后是这样 - 如果你的排序标准变得更复杂,lambda方法将允许你处理sql方法不会的其他和深奥的条件。

Performance-wise, they should be about equal, for a simple condition like this.

在性能方面,对于像这样的简单条件,它们应该大致相同。

#4


Well, if performance is very important, sort it on the SQL DB before getting the data.

好吧,如果性能非常重要,请在获取数据之前在SQL DB上对其进行排序。

#1


I'll prefer the first option.

我更喜欢第一种选择。

1) For code the readability point-of-view i think that Lambda is clearer than the second one.

1)对于代码,可读性的观点我认为Lambda比第二个更清晰。

2) In the first case you are using a strongly type way to sort your entity which is good.

2)在第一种情况下,您使用强类型方式对您的实体进行排序,这是好的。

3) On the second case you are passing the field and the order in a string mmmm dont like it.

3)在第二种情况下,你传递的字段和字符串mmmm不喜欢它。

Go Lambdas!!!

Regards!

#2


What's the purpose for the question? Performance is almost certainly a non-issue; if not, you'll need to test it in your context. I think if you are specifying a sort for a SQL statement it should be in the SQL statement; and I prefer to avoid constructing SQL statements stringwise if possible. Which do you like better esthetically?

这个问题的目的是什么?表现几乎肯定不是问题;如果没有,你需要在你的上下文中测试它。我想如果你为SQL语句指定一个排序,它应该在SQL语句中;如果可能的话,我更愿意避免以字符串方式构造SQL语句。你更喜欢哪种美学?

#3


To throw my 2c into the pot:

把我的2c扔进锅里:

Coming from an sql background, I personally find the sql way more intuitive - but that's no reason to follow it. The lambda method has several good recommendations for it:

来自sql背景,我个人觉得sql方式更直观 - 但是没有理由遵循它。 lambda方法有几个很好的建议:

As MRFerocius (is the missing 'o' intentional?) points out - the lambda method is strongly typed. That's a good thing.

正如MRFerocius(缺少'o'故意?)指出的那样 - lambda方法是强类型的。这是好事。

And then there's this - if your sorting criteria should ever become more complicated, the lambda method would allow you to tack on additional and esoteric conditions that the sql method would not.

然后是这样 - 如果你的排序标准变得更复杂,lambda方法将允许你处理sql方法不会的其他和深奥的条件。

Performance-wise, they should be about equal, for a simple condition like this.

在性能方面,对于像这样的简单条件,它们应该大致相同。

#4


Well, if performance is very important, sort it on the SQL DB before getting the data.

好吧,如果性能非常重要,请在获取数据之前在SQL DB上对其进行排序。