2016-04-08 80 views
0

我必須建立與上課時間,daysofweek和課程的健身房時間表,所以我是從數據庫中獲取是:我怎樣才能從KeyValuePair值值

時間 - 星期幾 - 類
08:00 - 1 - Natação
08:00 - 3 - Natação
08:30 - 1 - 柔術
09:00 - 1 - Natação
10:30 - 1 - 柔術
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經驗。如果任何人都可以提出我需要做的事情,那我真的很感激。

在此先感謝。

回答

1

每個KeyValuePair將包含一個.Value屬性,它會給你的值。請參閱此MSDN https://msdn.microsoft.com/en-us/library/ms224761(v=vs.110).aspx

編輯後更好地理解這個問題:

你的問題是雙重的。

  1. 首先,您正在構建您的查詢錯誤的所需輸出類型。
  2. 第二,您正在循環查看您的視圖中的錯誤信息,以便根據需要進行顯示。

如果我是你,我會擺脫Group類,因爲這是不再需要的。 A 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鍵,則將Aulas添加到Dictionary中的現有條目中。這樣,當我在視圖中遍歷它們時,我正在通過DateTime循環。然後對於每個列(<td>),我在.Value上執行一個Where子句,它將是List<Aulas>,並使用Select查詢來獲取我想要的數據類型。它將取出所有具有該特定idDias的Aulas,並使用String.Join()方法將它們加入。

+0

感謝您的評論。我已經看到了這個MSDN,但正如我寫的,我沒有使用keypair的經驗,我不知道如何解決這個問題。你可以幫幫我嗎? – Rodney

+0

@Rodney哪個變量是你的KeyValuePair? – Mikanikal

+0

對不起,我沒有得到你要求的。 – Rodney