I am using ASP.NET Core with an EF Model. I would like to display a number of rows by Date and then by Time, but I only want 5 records. What am concerned with is it actually ordering all the rows first before taking the 5 records or is it taking the 5 rows from the table and then ordering them. What I need is the former, them to be ordered, than take the 5 records from that already ordered list in linq.
I have tried:
Model.Meetings.OrderBy(x => x.Date).ThenBy(x => x.Time).Take(5)
and also,
Model.Meetings.Take(5).OrderBy(x => x.Date.ThenBy(x => x.Time)
The first one doesn't seem to display or actually does not return anything.
The second one works and shows 5 records, but what 5 records?
@foreach (var meeting in Model.Meetings.Take(5).OrderBy(x => x.Date).ThenBy(x => x.Time))
{
@if (meeting.Date > DateTime.Today)
{
<tr>
<td>@meeting.Name</td>
<td>@meeting.Date</td>
<td>@meeting.Time</td>
</tr>
}
}
I expect to get 5 records to display in order by Date then by Time, but order the records first before displaying them.
Here is example data set:
Id Date Day IsActive Location ThisEventId Time
1 2019-07-19 00:00:00.0000000 Friday 1 On-Campus 2 09:15:00.0000000
2 2019-07-19 00:00:00.0000000 Friday 1 On-Campus 3 09:00:00.0000000
3 2019-07-19 00:00:00.0000000 Friday 1 On-Campus 4 09:30:00.0000000
4 2019-07-19 00:00:00.0000000 Friday 1 On-Campus 5 10:30:00.0000000
5 2019-07-19 00:00:00.0000000 Friday 1 On-Campus 6 11:00:00.0000000
6 2019-07-19 00:00:00.0000000 Friday 1 On-Campus 7 11:30:00.0000000
7 2019-07-19 00:00:00.0000000 Friday 1 On-Campus 8 13:00:00.0000000
8 2019-06-20 00:00:00.0000000 Thursday 1 On-Campus 9 08:45:00.0000000
9 2019-06-20 00:00:00.0000000 Thursday 1 On-Campus 10 09:00:00.0000000
10 2019-06-20 00:00:00.0000000 Thursday 1 On-Campus 11 09:30:00.0000000