LinqToQuerystring icon indicating copy to clipboard operation
LinqToQuerystring copied to clipboard

Filters on sub-properties / model binding a filter Predicate

Open mcintyre321 opened this issue 11 years ago • 3 comments

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?

mcintyre321 avatar Aug 28 '14 09:08 mcintyre321

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)

beyond-code-github avatar Aug 28 '14 09:08 beyond-code-github

Oh hang on a moment, do you mean filtering just the sub-collection and leaving the rest intact?

beyond-code-github avatar Aug 28 '14 09:08 beyond-code-github

Yes, that's the requirement

mcintyre321 avatar Aug 28 '14 10:08 mcintyre321