I have a service function to query a SQL db like so:
public IQueryable<MyModel> getAll()
{
IQueryable<MyModel> models = (from f in db.MyModel select f);
return models;
}
When I implement it in my controller it works when I chain the Take():
var models = myModelEntities.getAll().Take(5);
return View(models); // returns 5 rows to the view
But like this it doesn’t:
var models = myModelEntities.getAll();
models.Take(5);
return View(models); // returns thousands of rows to the view
Why is Take() ignored if it’s not chained? I have Lazy Loading enabled on my model…
It does work, but you’re not assigning the result of
Take()to anything.Take()doesn’t mutate themodelsvariable, it returns the top 5 items in the enumerable.The following would work: