user12813863
user12813863

Reputation:

Ordering data in order - SQL Server Management Studio

I write data (Login/Year/month/Day)[id is IDENTITY(1,1)] to the database by EF (asp.net, ordered list, day by day)

Controller asp.net save

await _ecpContext.Karta.AddRangeAsync(objKartaModel);
await _ecpContext.SaveChangesAsync();

This is how data in the database is displayed in SQL Server Management Studio

Id | Login | Year | Month | Day
---+-------+------+-------+-----
 1 | MIPA  | 2020 |   1   |  1
 2 | MIPA  | 2020 |   1   | 29
 3 | MIPA  | 2020 |   1   | 28
 4 | MIPA  | 2020 |   1   | 27
...
15 | MIPA  | 2020 |   1   | 30

Later I download this data in the program and they are not ordered (not day by day)

Question: how can I organize this data so that it displays 'day after day'?

Upvotes: 0

Views: 87

Answers (2)

JMP
JMP

Reputation: 1934

_ecpContext.Karta.OrderBy(x=> x.Year).ThenBy(x => x.Month).ThenBy(x =>  x.Day);

Upvotes: 2

Kurt Hamilton
Kurt Hamilton

Reputation: 13515

Add an order by to the end of the query in management studio:

ORDER BY Year, Month, Day

Edit:

If no explicit order is specified, sql server will order the results in the way it thinks is best - generally by an index.

Edit 2:

If you want to order the results in EF, then add this to your query:

.OrderBy(x => x.Year).ThenBy(x => x.Month).ThenBy(x => x.Day)

Amending where your property names may differ.

Upvotes: 2

Related Questions