Reputation: 33
I have an SQLDatareader that return data in the format:
58, A1 59, A2 60, A3 61, A4 62, B1 63, B2 64, B3 65, B4 66, C1 67, C2 68, C3 69, C4 70, D1 71, D2 72, D3 73, D4
This can continue for more records. I want to return those in a different order (both fields). Like this:
58, A1 62, B1 66, C1 70, D1 59, A2 63, B2 67, C2 71, D2 61, A3 64, B3 68, C3 72, D3 61, A4 65, B4 69, C4 73, D4
Is there a way to do it?
Thank you in advance, Dimitris
Upvotes: 1
Views: 183
Reputation: 53958
As it is stated here:
A SqlDataReader
class:
Provides a way of reading a forward-only stream of rows from a SQL Server database.
That being said, if you don't want to change your sql statement or if you don't allowed to do so, you should first store your results in memory and then make that you want.
One option it would be to define a class like the below one:
// The name of the class is vague. Not knowing
// what your data represent, is quite difficult to
// give a proper name.
public class Result
{
public int Number { get; private set; }
public string Text { get; private set; }
public Result(int number, string text)
{
Number = number;
Text = text;
}
}
Then I would define a list of object of type Result
:
var results = new List<Result>();
After this in the while
loop, where you read your data, I would create in each loop an object of type Result
:
var result = new Result(..,...);
and then we add this at the results
list.
At then end you would have a list of Result
objects which you could sort as you wish. This could be done fairly easily using LINQ
.
var results = results.OrderBy(result=>result.Text[1]).ToList();
You could check the above it this .NET Fiddle
Upvotes: 2