I want to apply a number of composite filters to a group of data like this:
Filter[] filters = new[] { new Filter { Name = "Bob", Gender = "Male" },
new Filter { Name = "Alice", Height = "Female" } };
_dbContext.People.Where(p => filter.Any(f => f.Name == p.Name && f.Gender == p.Gender)).Select(p => p.Id);
I'm interested in the Ids
of Male Bobs, and Female Alices. Sorry Female Bobs. I don't want you.
This is the correct way of solving that problem in memory Linq, but there's a problem. This is what the SQL EF generates looks like (I'm checking this in my SQL server profiler)
SELECT [p].[Name], [p].[Gender], [p].[Id] FROM [People] AS [p]
This is terrible. It digs up everything and then does the actual work in memory. There is no way this will work with a lot of people, it will grind to a halt.
Is there any way to make the generated sql look more like this?
SELECT
[Person].[Id]
FROM [Person]
WHERE
((([Person].[Name] = "Bob") AND ([Person].[Gender] = "Male"))
OR (([Person].[Name] = "Alice") AND ([Person].[Gender] = "Female")))
(as is possible in Dapper)