Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter one-to-many relationship #154

Open
flyspirit99 opened this issue Aug 4, 2021 Discussed in #153 · 2 comments
Open

Filter one-to-many relationship #154

flyspirit99 opened this issue Aug 4, 2021 Discussed in #153 · 2 comments

Comments

@flyspirit99
Copy link

Discussed in #153

Originally posted by flyspirit99 August 4, 2021
I started to use Sieve a couple of days ago and love it. Now I encounter a situation and don't know how to do it by Sieve

I have a one to many relationship like below

public class Company
{
  public ICollection<Certificate> Certificates {get; set;}
}

public class Certificate
{
    public string Name {get; set;}
    public int CompanyId { get; set;}
    public Company Company {get; set;}
}

In CompanyApiController.GetCompanies() action, I need to filter company by its certificate name. Is this possible?

@ameckl
Copy link

ameckl commented Nov 4, 2021

I'm don't know if that is possible using SievePropertyMapper, but you can implement the ISieveCustomFilterMethods interface.

public IQueryable<Company> CertificateName(IQueryable<Company> source, string op, string[] values)
{
    return source.Where(company => company.Certificates.Any(certificate => values.Contains(certificate.Name)));
}

Take at look at: https://github.com/Biarity/Sieve#add-custom-sortfilter-methods

@bauermalzwei
Copy link

I can confirm what @ameckl suggested. I had the same challange and it works fine with ISieveCustomFilterMethods
You just have to use the method name in the URL e.g.: /api/companies?page=1&pagesize=10&filters=CertificateName==MyFancyCertificate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants