0

I have a crude weather dashboard built with ASP.NET Core and C# (MVC). Right now I have the View displaying the data from the API Call by means of a foreach loop:

            @foreach (var forecast in Model.list)
            {
            
                        <tr>
                       
                            <td>@(TimeUtils.UnixTimeStampToDateTime(forecast.dt).ToLongDateString())</td>
                            <td>@forecast.main.temp_min F°</td>
                            <td>@forecast.main.temp_max F°</td>
                            <td>@forecast.main.temp F°</td>
                            <td>@forecast.main.feels_like</td>

                        </tr>
                

            }

The data I want to display from the API call is stored in a list in the model:

    public class CityModel
    {
        public List<List> list { get; set; }
    }

This list has weather data for every 3 hours over a 5 day period. When the foreach loop finishes iterating over the List data, it displays total of 40 entries in the table.

Weather forecast for Enid 
date                          min       max        temp        feels_like
Monday, October 18, 2021    68.99 F°    69.85 F°    68.99 F°    67.37
Monday, October 18, 2021    70.16 F°    72.52 F°    70.16 F°    68.52
Monday, October 18, 2021    66.36 F°    67.24 F°    67.24 F°    65.41
Monday, October 18, 2021    63.55 F°    63.55 F°    63.55 F°    61.54
Tuesday, October 19, 2021   60.76 F°    60.76 F°    60.76 F°    58.8
Tuesday, October 19, 2021   59.74 F°    59.74 F°    59.74 F°    57.76
cont...

What I would like to know is how would I go about getting the table to display only every 8th entry/ index (making it, in effect, a daily forecast) as opposed to every entry/index? I gather it would somehow involve using the condition:

if( index % 8 == 0) {
           <tr>
                       
                            <td>@(TimeUtils.UnixTimeStampToDateTime(forecast.dt).ToLongDateString())</td>
                            <td>@forecast.main.temp_min F°</td>
                            <td>@forecast.main.temp_max F°</td>
                            <td>@forecast.main.temp F°</td>
                            <td>@forecast.main.feels_like</td>

           </tr>
}

but beyond this, I'm in the dark.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    Do you know how to do an indexed for loop? I.e., `for (int i = 0; i < Model.list.Length; i += 8) {` (or `Model.list.Count`, or `Model.list.Count()`, depending on the type of `Model.list`). That will get every 8th element. – Heretic Monkey Oct 18 '21 at 18:21
  • @Heretic Monkey I'm trying it out right now, so far I haven't been able to get the data in the table to register (without the forecast parameter) – Adam Cushing Oct 18 '21 at 18:28
  • You need more than the code I posted. That was just example code to get you started. You need something like `var forecast = Model.list[i];` – Heretic Monkey Oct 18 '21 at 18:30
  • @Heretic Monkey That did the trick! Much obliged. I had that older post open in my browser but I got confused with their discussion of LINQ. It looks like ``` var forecast = Model.list[i];``` was the missing ingredient from what I had tried previously before posting. – Adam Cushing Oct 18 '21 at 18:37

1 Answers1

-1

You can create a new list that consists of only the items you want to display such as:

var forecastList = Model.list.Where((x,i) => i % 8 == 0);
@foreach (var forecast in forecastList)
David Jones
  • 2,879
  • 2
  • 18
  • 23
  • This technique is discussed in [the linked duplicate](https://stackoverflow.com/questions/682615/how-can-i-get-every-nth-item-from-a-listt)... – Heretic Monkey Oct 18 '21 at 18:40