我必须用课时,天数和课程来建立健身房时间表,所以我从数据库中得到的是:
时间 - Dayofweek - 上课
08:00 - 1 - Natação
08:00 - 3 - Natação
08:30 - 1 - Jiu-jitsu
09:00 - 1 - Natação
10:30 - 1 - Jiu-jitsu
14:00-3:Natação
我的模特:
public class Group<K, T>
{
public K Key;
public IEnumerable<T> Values;
}
public class Aulas
{
public int id { get; set; }
public int idDias { get; set; }
public DateTime AulaHora { get; set; }
public string AulaNome { get; set; }
}
我的控制器
public ActionResult Index()
{
Context _q = new Context();
var query = from x in _q.Aulas
group x by x.AulaHora into g
select new Group<DateTime, Aulas> { Key = g.Key, Values = g };
return View(query.ToList());
}
我的观点:
@using Grid.Models;
@model List<Group<DateTime, Aulas>>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<table class="table table-striped">
<thead><tr><th>Horários</th><th>Segunda</th><th>Terça</th><th>Quarta</th><th>Quinta</th><th>Sexta</th><th>Sábado</th></tr></thead>
@foreach (var item in Model)
{
<tr>
@foreach (var i in item.Values)
{
<td>@i.AulaHora.ToString("HH:mm")</td>
<td>@((i.idDias == 1) ? i.AulaNome.ToString() : "")</td>
<td>@((i.idDias == 2) ? i.AulaNome.ToString() : "")</td>
<td>@((i.idDias == 3) ? i.AulaNome.ToString() : "")</td>
<td>@((i.idDias == 4) ? i.AulaNome.ToString() : "")</td>
<td>@((i.idDias == 5) ? i.AulaNome.ToString() : "")</td>
<td>@((i.idDias == 6) ? i.AulaNome.ToString() : "")</td>
}
</tr>
}
</table>
不幸的是,我没有使用KeyValuePair的经验。如果有人可以提出我需要做的事情,那我真的很感激。
提前致谢。
答案 0 :(得分:1)
每个KeyValuePair都将包含一个.Value属性,该属性将为您提供值。请参阅此MSDN https://msdn.microsoft.com/en-us/library/ms224761(v=vs.110).aspx
在更好地理解问题后编辑:
你的问题有两个问题。
如果我是你,我将摆脱那个不再需要的Group
课程。 Dictionary
更适合您的需求。我会将您的模型设置为Dictionary<DateTime, List<Aulas>>
。
Dictionary<DateTime, List<Aulas>> query =
new Dictionary<DateTime, List<Aulas>>();
_c.Aulas.ForEach(
(a) =>
{
if (query.ContainsKey(a.AulaHora))
{
query[a.AulaHora].Add(a);
}
else
{
query.Add(a.AulaHora, new List<Aulas>() { a });
}
});
return View(query);
然后,您需要以不同方式循环播放它们以获得所需的结果。
@using Grid.Models;
@model Dictionary<DateTime, List<Aulas>>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<table class="table table-striped">
<thead><tr><th>Horários</th><th>Segunda</th><th>Terça</th><th>Quarta</th><th>Quinta</th><th>Sexta</th><th>Sábado</th></tr></thead>
@foreach (var item in Model)
{
<tr>
<td>@(item.Key.ToString("HH:mm"))</td>
<td>@(string.Join(", ", item.Value.Where((wa) => wa.idDias == 1).Select((sa) => sa.AulaNome)))</td>
<td>@(string.Join(", ", item.Value.Where((wa) => wa.idDias == 2).Select((sa) => sa.AulaNome)))</td>
<td>@(string.Join(", ", item.Value.Where((wa) => wa.idDias == 3).Select((sa) => sa.AulaNome)))</td>
<td>@(string.Join(", ", item.Value.Where((wa) => wa.idDias == 4).Select((sa) => sa.AulaNome)))</td>
<td>@(string.Join(", ", item.Value.Where((wa) => wa.idDias == 5).Select((sa) => sa.AulaNome)))</td>
<td>@(string.Join(", ", item.Value.Where((wa) => wa.idDias == 6).Select((sa) => sa.AulaNome)))</td>
</tr>
}
</table>
只是一些事情。我正在创建一个基于Aulas的DateTime的字典。如果有匹配的DateTime键,那么我将Aula添加到Dictionary中的现有条目。这样,当我在View中循环它们时,我正在按DateTime循环。然后对于每个列(<td>
),我在.Value上执行Where子句,它将是List<Aulas>
,并且选择查询以获取我想要的数据类型。它将拉出所有具有该特定idDias的Aula并使用String.Join()方法将它们连接起来。