So I have this LINQ query that ends in a custom select kinda like this:
select new { this1 = table.this1, this2 = othertable.this2 }
The call to that query from the Controller looks something like this:
ViewData["these"] = theRepo.GetAllThese(someVar, anotherVar);
Now when I pass this on to my view since it is not strongly typed how can I iterate through it with a foreach, how can I cast it as an IQueryable or a List if I don’t know what’s in it?
…is it something like this?
IQueryable<???> these = ViewData["These"];
foreach (var this in these) {...
Just need to know what to put for ‘???’ I think.
Your linq query returns a collection of anonymously typed objects. Being anonymous, there is no way to “call their name” when declaring an explicitly typed variable. Thus, the true type/shape of the objects is only known within the action method where the objects are defined.
The indexed getter of the
ViewDataobject has a return type ofobject, and without knowing the type name, you want be able to cast the return value ofViewData["these"]to anything useful.What you might want to do instead, is to create a model – more specifically a “view model” – which defines the structure of the objects you are selecting using LINQ:
and redefine your query to do a select like this:
Your objects now share a common named class, and your collection can be easily casted to the proper type in the view.