I’m trying to create a linq query based on some dynamic/optional arguments passed into a method.
User [Table] -> zero to many -> Vehicles [Table] User [Table] -> zero to many -> Pets
So we want all users (including any vechile and/or pet info). Optional filters are
- Vehicle numberplate
- Pet name
Because the vehicle and pet tables are zero-to-many, i usually have outer joins between the user table and the vehicle|pet table.
To speed up the query, i was trying to create the dynamic linq and if we have an optional argument provided, redfine the outer join to an inner join.
(The context diagram will have the two tables linked as an outer join by default.)
Can this be done?
I’m also not sure if this SO post can help me, either.
I think you are heading in the wrong direction. You can easily use the fact that LINQ queries are composable here.
First, you would always use the outer join, and get all users with the appropriate vehicles and pets:
Then you would add the filters if necessary:
Note that this will not filter out the pets or cars that don’t meet the filter, as it is simply looking for the users that have pets with that name, or cars with that plate.