What is the best for sorting a data table in c#, both from a performance and a code-readability point-of-view:
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?
-
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?
-
I'll prefer the first option.
1) For code the readability point-of-view i think that Lambda is clearer than the second one.
2) In the first case you are using a strongly type way to sort your entity which is good.
3) On the second case you are passing the field and the order in a string mmmm dont like it.
Go Lambdas!!!
Regards!
-
Well, if performance is very important, sort it on the SQL DB before getting the data.
GenericMeatUnit : The poster states that the data source is a sharepoint list and that he is aware of, but cannot use, the sql ORDER BY method - although I don't know if that was written before or after your answer. -
To throw my 2c into the pot:
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:
As MRFerocius (is the missing 'o' intentional?) points out - the lambda method is strongly typed. That's a good thing.
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.
Performance-wise, they should be about equal, for a simple condition like this.
0 comments:
Post a Comment