Is there a way to freeze an IQueryable so that no additional joins will be added to the query when hitting the database? For example, I could do a .ToList() to freeze the query, but that has performance impacts because any filtering I do is on the middle layer and I don't have any performance gains from pre-filtering on the db server?
Edit for clarity:
I have an OData service which returns an IQueryable that the client can filter/sort/project as needed. I just want to prevent them from pulling more data out. I could do that by doing ToList().AsQueryable(), but that loses the advantage of lazyLoading, and with it, the whole purpose of allowing the client to filter the request.
One option that I looked at was to set: EnableQueryAttribute.AllowedQueryOptions to exclude Expand, however even if my initial Query had been expanded, the client is still prevented from selecting those parts.
Im assuming that you actually have an
IQueryable<T>instead of aIQueryable.If you don't want your client having access to all
IQueryable<T>methods, don't return anIQueryable<T>. Since you want them to be able to only filter/sort/project, create a object witch contains anIQueryable<T>but only expose the desired methods, and use that:That way you can also prevent EF and DB related dependencies creeping up on your application. Any change on
IQueryable<T>methods will be contained within this class, instead of spread all over the place.