简体   繁体   中英

C#: Combining for(each) loops to efficient code

This is part of my timetable code

Two questions :

  1. How can I prevent having to loop through the times, and then the roombookings to check if it exists in the given time slot. Is there a way of just saying "for this time, is there a match(s) from this list and what is its Id"

  2. Is there anyway I can set the outer td to class bgred, rather than a span. (Note that there may be multiple bookings in a timeslot, hence having the td out of the foreach loop.)

<tr>
    <th></th>

        @for (int i = 9; i <= 17; i++)
        {
        <th>@i:00</th>
        }
</tr>

    @foreach (var RoomNo in ViewBag.Rooms)      
    {

        <tr>

            <th>@RoomNo</th>

           @for (int i = 9; i <= 17; i++)
           {
               <td>
               @foreach (var roombooking in ViewBag.RoomBookings)
               {
                   DateTime DateCheckStart = DateTime.Parse(ViewBag.Date.ToShortDateString() + " " + i.ToString() + ":00");
                   DateTime DateCheckEnd = DateTime.Parse(ViewBag.Date.ToShortDateString() + " " + i.ToString() + ":59");
                   if (DateCheckStart < roombooking.EndDateTime && DateCheckEnd > roombooking.StartDateTime && roombooking.RoomNo == RoomNo)
                   {
                     <span class="bgred"> @roombooking.RentalNo</span>
                   }
               }
               </td>
           }
        </tr>

     }

What about something like this

@foreach (var roombooking in ViewBag.RoomBookings.Where( /* in range */ ))

So that you can remove your first for loop

You should be filtering your data in the controller and only passing relevant data to the view.

You can use linq to objects in the controller, or even better filter data in the database.

Try something like this.

 <table>
<tr>
    <th></th>

        @for (int i = 9; i <= 17; i++)
        {
        <th>@i:00</th>
        }
</tr>

@{ List<MyLibrary.MyRoomOccupancy> RoomBookingsTemp = ViewBag.RoomBookings;}

@foreach (var RoomNo in ViewBag.Rooms)
{
    <tr>
        <th>@RoomNo</th>

       @for (int i = 9; i <= 17; i++)
       {             
           DateTime DateCheckStart = DateTime.Parse(ViewBag.Date.ToShortDateString() + " " + i.ToString() + ":00");
           DateTime DateCheckEnd = DateTime.Parse(ViewBag.Date.ToShortDateString() + " " + i.ToString() + ":59");

           var sublist = (from RoomOcc in RoomBookingsTemp where DateCheckStart < RoomOcc.EndDateTime && DateCheckEnd > RoomOcc.StartDateTime && RoomOcc.RoomNo == RoomNo select RoomOcc);


           if (sublist.Count() > 0)
           {
              @Html.Raw("<td class= \"bgred\">");

            }
           else {
               Html.Raw("<td>");
           }

           foreach (var roombooking in sublist)
           {
               if (DateCheckStart < roombooking.EndDateTime && DateCheckEnd > roombooking.StartDateTime && roombooking.RoomNo == RoomNo)
               {
                  @roombooking.RentalNo
               }
           }
           @Html.Raw("</td>");

       }
    </tr>

}


</table>

What this does is it loops through onlyt he

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM