I'm trying to create a dynamic filter using a combination of lambda functions. But the number of lambda functions could vary based on the number of different filters the user applies.
I want something that behaves like this
//images is a List<ImageInfo>
var result1 = images
.Where(image => image.Bytes < 1000)
.Where(image => image.Height < 100)
.Where(image => image.Width < 100);
Where the height filter is only applied to those images that pass the bytes filter. And the width filter is only applied by to those images that pass the height filter.
But, the dynamic fashion of how the filters are turned on and off by the users doesnt allow me to create a single lambda function.
Instead I'll create a list of lambda functions and then need to apply them to the list of images. So I would end up with something like this; multiple individual lambda functions.
var filter1 = images.Where(image => image.Bytes < 1000);
var filter2 = images.Where(image => image.Height < 100);
var filter3 = images.Where(image => image.Width < 100);
How can I join multiple lambda functions together to get my final list of filtered images?
I did this
var result = filter1.Intersect<ImageInfo>(filter2).Intersect<ImageInfo>(filter3);
But each filter spins through the main list of images to get its subset of images, and then does an intersection calculation, which takes way too much CPU.
So what I'm looking for is a way to take an arbitrary list of lambda functions (Expressions...whatever) and join them in a way that gets executed the way the first example I showed does.