0

我的代码在视图中:

@foreach (var item in Model._games)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>

            <td>
                @foreach (var item2 in Model._days.Where(x => x.Games.Any(u=> u.Id == item.Id)))
                {
                    @item2.CutOffTime;
                }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
            </td>
        </tr>
    }

显示第二个 foreach 并像我一样过滤它的正确方法是什么?如果我不过滤(仅使用Model._days,它会显示所有天数,但是当我使用带 ID 的 Where 过滤器时,正如您在第二个 foreach 中看到的那样,它不会显示任何内容。

谢谢

4

2 回答 2

0

Possible issues:
1. You're using deferred loading and forgot to populate .Games property of Model._days by using of Include method (EF) or LoadWith (LINQ 2 SQL)
2. Games is empty
3. Games is not empty but you've set an incorrect filter
4. You've populated either Model._games or Model._days.Games with incorrect data

Troubleshooting: set a breakpoint at the second foreach and see what actual values do you have in both collections.

于 2013-11-15T02:14:06.367 回答
0
  • 将您的第二个 foreach 更改为

     @foreach (var item2 in Model._days)
     {
       if(item2.Games.select(e=>e.Id).Cotains(item.Id)
      {
         @item2.CutOffTime;
      }
     } 
    
于 2013-11-15T03:35:05.613 回答