Does something like this affect performance badly?
var myQuery = from c in Customers select c; var filter1 = from c in myQuery where c.ID > 2 select c; myQuery = filter1; var filter2 = from c in myQuery where c.Name.Contains('r') select c; myQuery = filter2;
When I do this it seems to only do the actual query at the end, not on every ‘var…’. Everything up to that point seems to just construct a query, so it seems like this is ok and not much performance difference from placing all the filters in 1 query. Am I wrong and it’s actually running multiple queries against the database?
I’m trying to find out a good way to create queries based on a user’s input so they can filter on different criteria. I’m not too worried about performance as long as doing this is not going to take a huge amount of time.
I also found posts about the Dynamic Linq library, but using that seems clunky and I don’t see much difference from doing it this way.
I would look at using Extension methods to construct the query dynamically. I think that it will do exactly what you need to do. And, yes, the query isn’t actually evaluated until an operation is performed that needs results so combining them does not necessarily result in additional trips to the database.