How to combine two LINQ Expressions?
c#, expression, linq
Solution
It's a bit ugly but if you want to keep them as expressions:
stats = stats.Where(s => (e1.Compile()(s) || e2.Compile()(s))).ToList();
If you can change them to `Func`s it's cleaner:
Func<Stat, bool> e1 = s => s.Age >= 15 && s.Age < 25;
Func<Stat, bool> e2 = s => s.Age >= 40 && s.Age < 50;
stats = stats.Where(s => e1(s) || e2(s)).ToList();
Problem
I have the following LINQ query in c# ``` var stats = from s in context.Stats select s; Expression<Func<Stat, bool>> e1 = s => s.Age >= 15 && s.Age < 25; Expression<Func<Stat, bool>> e2 = s => s.Age >= 40 && s.Age < 50; stats = stats.Where(e1); ``` This code works and gives me the rows from the Stats table where Age is between 15 an 25. Now, i would like to get the rows from 15 to 25 AND from 40 to 50. How do I combine those 2 expressions ? thanks