2014-01-12 51 views
2

我想GROUP BY場「TimeName」歸還物品,所以我不想在每行重複場TimeName,代碼如下:集團項目的for循環(沒有SQL)內

@foreach (var item in Model) { 


     tim = item.MealTime; 


@Html.DisplayFor(modelItem => item.TimeName) 
<text> , </text>   
    @Html.DisplayFor(modelItem => item.MealTime) 

     <br />  
      @Html.DisplayFor(modelItem => item.FoodName) 
      @Html.DisplayFor(modelItem => item.Quantity) 
      @Html.DisplayFor(modelItem => item.FoodUnit) 

      @Html.DisplayFor(modelItem => item.MealDate) 
      @Html.DisplayFor(modelItem => item.MealTimeSpecific) 
      @Html.DisplayFor(modelItem => item.IsActive) 
      @Html.DisplayFor(modelItem => item.InsertDate) 
      @Html.DisplayFor(modelItem => item.InsertUser) 

    <br /> 
} 

我試圖做嵌套for循環,但它不起作用,有沒有辦法解決這個問題?

回答

1

我找到了一個解決辦法,但我希望能有更好的一個,

@{ 
     tim = Model.FirstOrDefault().MealTime; 
    } 

    @Model.FirstOrDefault().TimeName 
    <br /> 

@foreach (var item in Model) { 
    if (tim != item.MealTime) 
    { 
@Html.DisplayFor(modelItem => item.TimeName) 
     <br /> 
} 

    tim = item.MealTime; 


      @Html.DisplayFor(modelItem => item.MealTime) 
      @Html.DisplayFor(modelItem => item.FoodName) 
      @Html.DisplayFor(modelItem => item.Quantity) 
      @Html.DisplayFor(modelItem => item.FoodUnit) 

      @Html.DisplayFor(modelItem => item.MealDate) 
      @Html.DisplayFor(modelItem => item.MealTimeSpecific) 
      @Html.DisplayFor(modelItem => item.IsActive) 
      @Html.DisplayFor(modelItem => item.InsertDate) 
      @Html.DisplayFor(modelItem => item.InsertUser) 

    <br /> 

} 
+0

我不會接受我的答案是「公認的答案:」如果有人發現有任何解決方案可以正常工作,請嘗試 –