Your example is a bit vague, but it's easy to create a method that returns an IQueryable<T> and reuse this method if that is what you mean. Here is an example:
// Reusable method public IQueryable<SomeObject> GetSomeObjectsByFilter(Context c) { return from someObject in context.SomeObjects where cBAAmount < 1000 where c.Roles.Contains(r => r.Name == "Admin") select someObject; }
You can reuse this method in other places, for example:
var q = from c in GetSomeObjectsByFilter(context) where !cDContains(d => d.Items.Any(i => i.Value > 100)) select c;
Since the IQueryable method works, only the final query (the collection that you start iterating) will call the database, which will allow you to create a system with a high degree of support, reusing business logic that runs efficiently inside the database, to lose any performance.
I do this all the time and it improves the maintainability of my code. It works no matter what O / RM tool you run, because there is no difference in the composition of the Queryable<T> between writing a query in one world or splitting it into different methods.
Note that sometimes you need some smart transformations to get duplicate parts in one method. Things that can help return grouped sets and return a set of a different type than what you think is needed. This sounds a bit eloquent, but just ask the question here when you have problems with splitting the method. There are enough people here who can help you.
source share