mattruma
mattruma

Reputation: 16677

LINQ with dynamic objects

Try to sort through a bunch of examples, and can't seem to make this work ... which leads me to believe I might be trying the impossible! :)

I have the following code in my view:

<fieldset>
    <legend>Map</legend>
    <table class="map">
        @for (var y = 0; y <= (int)ViewBag.Map.Height; y++)
        {
            <tr>
                @for (var x = 0; x <= (int)ViewBag.Map.Width; x++)
                {
                    <td>@ViewBag.Map.MapTiles.Where(z => z.X == x && z.Y == y)</td>
                }
            </tr>                
        }
    </table>
</fieldset>

Map is a dynamic object.

When I run this, I get the following error:

Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

Is there a way to get around this and keep my LINQ query simple?

Upvotes: 0

Views: 979

Answers (1)

Wiktor Zychla
Wiktor Zychla

Reputation: 48230

Cast first, as the compiler says

   <td>@((IEnumerable<MapTileOrWhat>)(ViewBag.Map.MapTiles)).Where(z => z.X == x && z.Y == y)</td>

Upvotes: 2

Related Questions