I’m using Fluent Mongo and having issues creating a dynamic linq query because Fluent Mongo doesn’t support Contains. I basically need to have a nested OR statement within my Where to check if an Enum matches a list of enums. I’m sure there’s another way to do this without using Contains, I just don’t know enought about linq… I’m assuming I need to separate the linq expressions out and add them dynamically but I can’t figure it out.
I’ve tried using Dynamic Linq (ScottGu) but that doesn’t seem to work with Enums, and I don’t see how you can add a dynamic amount of where statements, the examples just show how to use dynamic values.
I’ve tested this to see if nested Or’s work, and they do as expected, I just can’t figure out how to build the nested Or’s dynamically:
candidates.Where(p => p.CreatedOn >= _startDate && p.CreatedOn <= _endDateTime && (p.SomeEnum == enmSomeEnum.Value1 || p.SomeEnum == enmSomeEnum.Value2));
Thank you,
Tim
First off I would agree that PredicateBuilder is a much simpler approach. However, if you want a lengthy solution that provides (in my opinion) more flexibility you can look at the below solution. I put it together for some work on generating dynamic OData queries and is compiled from several other sources (see the links in the code). It provides a generic way to apply an IsIn list to a Queryable. The code you want is in the QueryExt class and the rest is just a example program showing how it works and one way how it might solve your problem.
}