我正在宿舍里为洗衣机开发预订系统。 它将是一个ASP.NET MVC应用程序。 我有一个班级WashingTime应该放在一张桌子上,这样用户可以点击它们并预定洗衣时间。
WashingTime类看起来像这样:
public class WashTime
{
public int ID { get; set; }
public DateTime Time { get; set; }
public bool IsBooked { get; set; }
public string Machine { get; set; }
public int RoomNumber { get; set; }
}
我使用MS SQL数据库作为后端。 我有时间表和预订系统工作,但我知道我这样做的方式不是最聪明的,所以视图(时间表)需要很长时间才能加载。 在视图中我使用了大量的Razor来检查模型对象是否被预订 - 是否有更聪明的方法来做到这一点,我想有必要用JavaScript来检查这个。 我的视图代码是:
@for (int i = 6; i < 24; i++)
{
<tr>
<th>@i.ToString():00 - @(1 + i):00</th>
<td>
<table class="col-xs-12">
<div class="inner-table">
<td>
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Monday && d.Machine == "Left").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Monday && d.Machine == "Left").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Monday && d.Machine == "Left").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Monday && d.Machine == "Left").Machine
</div>
}
</div>
</td>
<td class="">
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Monday && d.Machine == "Right").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Monday && d.Machine == "Right").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Monday && d.Machine == "Right").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Monday && d.Machine == "Right").Machine
</div>
}
</div>
</td>
</div>
</table>
</td>
<td>
<table class="no-padding col-xs-12">
<div class="inner-table">
<td>
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Tuesday && d.Machine == "Left").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Tuesday && d.Machine == "Left").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Tuesday && d.Machine == "Left").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Tuesday && d.Machine == "Left").Machine
</div>
}
</div>
</td>
<td class="">
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Tuesday && d.Machine == "Right").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Tuesday && d.Machine == "Right").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Tuesday && d.Machine == "Right").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Tuesday && d.Machine == "Right").Machine
</div>
}
</div>
</td>
</div>
</table>
</td>
<td>
<table class="no-padding col-xs-12">
<div class="inner-table">
<td>
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Wednesday && d.Machine == "Left").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Wednesday && d.Machine == "Left").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Wednesday && d.Machine == "Left").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Wednesday && d.Machine == "Left").Machine
</div>
}
</div>
</td>
<td class="">
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Wednesday && d.Machine == "Right").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Wednesday && d.Machine == "Right").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Wednesday && d.Machine == "Right").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Wednesday && d.Machine == "Right").Machine
</div>
}
</div>
</td>
</div>
</table>
</td>
<td>
<table class="no-padding col-xs-12">
<div class="inner-table">
<td>
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Thursday && d.Machine == "Left").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Thursday && d.Machine == "Left").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Thursday && d.Machine == "Left").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Thursday && d.Machine == "Left").Machine
</div>
}
</div>
</td>
<td class="">
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Thursday && d.Machine == "Right").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Thursday && d.Machine == "Right").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Thursday && d.Machine == "Right").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Thursday && d.Machine == "Right").Machine
</div>
}
</div>
</td>
</div>
</table>
</td>
<td>
<table class="no-padding col-xs-12">
<div class="inner-table">
<td>
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Friday && d.Machine == "Left").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Friday && d.Machine == "Left").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Friday && d.Machine == "Left").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Friday && d.Machine == "Left").Machine
</div>
}
</div>
</td>
<td class="">
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Friday && d.Machine == "Right").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Friday && d.Machine == "Right").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Friday && d.Machine == "Right").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Friday && d.Machine == "Right").Machine
</div>
}
</div>
</td>
</div>
</table>
</td>
<td>
<table class="no-padding col-xs-12">
<div class="inner-table">
<td>
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Saturday && d.Machine == "Left").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Saturday && d.Machine == "Left").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Saturday && d.Machine == "Left").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Saturday && d.Machine == "Left").Machine
</div>
}
</div>
</td>
<td>
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Saturday && d.Machine == "Right").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Saturday && d.Machine == "Right").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Saturday && d.Machine == "Right").RoomNumber
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Saturday && d.Machine == "Right").Machine
</div>
}
</div>
</td>
</div>
</table>
</td>
<td>
<table class="no-padding col-xs-12">
<div class="inner-table">
<td>
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Sunday && d.Machine == "Left").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Sunday && d.Machine == "Left").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Sunday && d.Machine == "Left").Machine
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Sunday && d.Machine == "Left").Machine
</div>
}
</div>
</td>
<td>
<div class="rid">
<div id="ID" style="display: none;" data-value="@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Sunday && d.Machine == "Right").ID"></div>
@if (Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Sunday && d.Machine == "Right").IsBooked == true)
{
<div class="booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Sunday && d.Machine == "Right").Machine
</div>
}
else
{
<div class="not-booked">
@Model.First(d => d.Time.Hour == i && d.Time.DayOfWeek == DayOfWeek.Sunday && d.Machine == "Right").Machine
</div>
}
</div>
</td>
</div>
</table>
</td>
</tr>
}
正如您所看到的,我正在使用Razor进行252次检查,并且我认为它必须减慢我的页面速度。
调用视图的控制器如下所示:
public ActionResult Index()
{
List<WashTime> times = db.WashTimes.Where(x => x.Time.Year == DateTime.Today.Year).ToList();
List<WashTime> thisWeek = times.Where(time => HelperFunctions.GetIso8601WeekOfYear(time.Time) == HelperFunctions.GetIso8601WeekOfYear(DateTime.Today)).ToList();
if (thisWeek.Count<(126*2))
{
FillWeek();
times = db.WashTimes.Where(x => x.Time.Year == DateTime.Today.Year).ToList();
thisWeek = times.Where(time => HelperFunctions.GetIso8601WeekOfYear(time.Time) == HelperFunctions.GetIso8601WeekOfYear(DateTime.Today)).ToList();
}
ViewBag.Weeknumber = HelperFunctions.GetIso8601WeekOfYear(DateTime.Now);
return View(thisWeek.ToList());
}
我期待着你的意见。
答案 0 :(得分:0)
你的问题是Model.First
需要很长时间才能找到匹配的第一个并发。每次调用First
都可能会检查列表中的每个项目。
您需要创建一些其他结构,以便检查。例如,Dictionary < int ,Dictionary<int, Dictionary<string, WashTime>>>
,其中第一个键是每周一天,第二个是小时,第三个是&#34;第一个&#34;或&#34;秒&#34;。
或者在此字段的搜索中对您的列表进行排序,然后只使用索引,信任以前的顺序。这里有一个例子。我没有尝试过,但是你可以得到这个想法。我从here(multiple groupby)和here(group by to remove duplicates)以及here(multiple order by)
中获取了linq函数List<WashTime> thisWeek = times.Where(
time => HelperFunctions.GetIso8601WeekOfYear(time.Time) == HelperFunctions.GetIso8601WeekOfYear(DateTime.Today)
).OrderBy(f => f.Time.DayOfWeek).ThenBy(f => f.Time.Hour).ThenBy(f.Machine
).GroupBy(
f=> new { f.Time.DayOfWeek, f.Time.Hour, f.Machine}
).Select(
group => group.First()
).ToList()