In the ViewModel, should I create a List or an IQueryable then use it in my View.
1 Question
EX:
In the ViewModel I have this property
public List<MyEntity> ListEntity {get;set;}
public IQueryable<MyEntity> ListIQEntity {get;set;}
Then In the view I Make a loop like that.
<ul>
foreach(var e in Model.ListEntity)
{
<li><%:e.MyInfo</li>
}
</ul>
<ul>
foreach(var e in Model.ListIQEntity )
{
<li><%:e.MyInfo</li>
}
</ul>
2 Question
In the repository, Is it better to return an IQueryable or a List of Entity.
Many thanks
If you’re going to be simply looping through the set, then it’s probably best that you use
IEnumerable<MyEntity>, which is essentially what you’re doing when you’re using a List but without limiting yourself to using a specific type.You shouldn’t really need to use an
IQueryable<MyEntity>at the view level as all the expensive querying should have already been done by the time the data gets to the View, and you shouldn’t be doing any real processing at that level—do it all in the Model, or if needs be in the Controller, but not in the View.