Suppose I want to fetch data into an anonymous object (i.e. fetch data from two or more tables and store it in a variable) at some part of my application development with a Linq to Entity query. Later I need a Razor View to access this fetched/anonymous data. What should I do here?
What I think is to design another ViewModel that holds corresponding properties of the anonymous type properties and make view/razor which is strongly typed to that ViewModel? Is this the right way to work with anonymous types in EF 4.1? i.e for each anonymous type make an ViewModel and store Fetched data into object of it.
An Anonymous type isn't intended for use outside the scope of the method so they are usually just disposable types which would only be ever used in that method.
I would favour loading the data into a regular class over a ViewModel to fully separate the Model from the Views. I would use a custom class to hold the fetched data and then in the Controller populate the ViewModel with that class and have a razor view strongly typed to the ViewModel. This is a little less compact but has better separation of concerns.