I've taken a look at this answer on how to dynamically create an OrderBy expression in Entity Framework. But I'd like to also build a dynamic Where expression. Something along the lines of this:
public IEnumerable<InventoryItem> GetAll(string filterBy, object value)
{
var results = new List<InventoryItem>();
using (var db = new InventoryDb())
{
if (QueryHelper.PropertyExists<InventoryItem>(filterBy))
{
var query = db.rri_InventoryItems.WhereByProperty(filterBy, value);
foreach(var item in query.Where(expr))
{
results.Add(ConvertItem(item));
}
}
}
return results;
}
Passing in the property to filter by and a value as ab object. Queryable has two methods for Where that both take two parameters, so I am not even sure which is the proper one.
And it's at this point I get a little more lost. I'm not sure how to refactor the original OrderByProerty method to provide a WhereByProperty. I know what I have here is completely wrong. I'm not sure what to do with it.
Ideally, I'd want to extend this even more by providing a collection of objects that could be used to build a query with ands and or operators.
You need the one that receives
Expression<Func<T, bool>> predicate
.Here is how you can build dynamically a predicate similar to
(T item) => item.Property == value
:I've tried to write it in such a way so you can step over the code in order to understand what it does. The only line that might need some explanation is:
This is a simple way of handling nested properties like
obj.Prop1.Prop2
etc. If you don't need such capability, you can simply use this instead:I didn't need nested properties (yet). I modified your code slightly and have this that is working: