Filters on sub-properties / model binding a filter Predicate
Hi Pete!
I've got a requirement to add take/skip/filter on a sub-property for an resource like this:
{
"Owner": "123",
// ... more properties ...
"Activities": [
{
"ActivityType": "sample string 1",
// ... more properties ...
},
{
"ActivityType": "sample string 2",
// ... more properties ...
}
],
"TotalCount": 2
}
Rather than add yet another parameter to the controller, is there any way to use L2QS to model bind an a filter for the activities collection? e.g.
public MyResponse(int ownerId, Func<Activity, bool> activitiesFilter) {
var owner = _owners.Single(o => o.OwnerId);
owner.Activities.RemoveAll(activitiesFilter);
return owner;
}
It would also be handy to be able to model bind Expression<Func<Activity, bool>> so I could pass it into a Queryable.
Or maybe is this already possible by doing a projection and mapping back to the original model type?
Have you tried using the collection operators? As far as I'm aware they should work in this situation eg (from the website):
Collection Operators any /Categories?$filter=Products/any(p: p/Rating lt 4) all /Categories?$filter=Products/all(p: p/Rating ge 7)
Oh hang on a moment, do you mean filtering just the sub-collection and leaving the rest intact?
Yes, that's the requirement