简体   繁体   English

C#:将(每个)循环组合到有效的代码中

[英]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" 有没有办法只是说“这个时候,这个列表中是否有匹配,它的Id是什么”

  2. Is there anyway I can set the outer td to class bgred, rather than a span. 无论如何,我可以将外部td设置为类bgred,而不是跨度。 (Note that there may be multiple bookings in a timeslot, hence having the td out of the foreach loop.) (请注意,时间段中可能有多个预订,因此将td从foreach循环中移除。)

<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 这样你就可以删除你的第一个for循环

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. 您可以将linq用于控制器中的对象,甚至可以更好地过滤数据库中的数据。

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 这样做是因为它只能通过他

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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