Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple clauses blocking the whole filter

I created a big filter which gets data from my frontend, compares it with the database and returns the right person back. My first problem is that i think that my solution is not correct, because later i need and operations too. Also my FilterByItem Clauses blocks itself(Commented in Code below).

My Filter looks like this:

//Login
[HttpPost("filter/")]
public async Task<IActionResult> Filter([FromBody] Filter user)
{
    //Variable für Personentypen als Liste machen
    return Ok(await _context
        .Personens
        //Obere Felder (nicht erweitert)
        .Where(p => p.Vorname.ToLower().Contains(user.vorname))
        .Where(p => p.Nachname.ToLower().Contains(user.nachname))
        .Where(p => p.Anrede.ToLower().Contains(user.anrede))
        .Where(p => p.Ort.ToLower().Contains(user.adresse) || p.Plz.Contains(user.adresse) ||
            p.Land.Contains(user.adresse) || p.Bundesland.Contains(user.adresse) ||
            p.Straße.Contains(user.adresse))
        .Where(p => p.Firmenbezeichnung.ToLower().Contains(user.firmenbezeichnung))
        .FilterByItems(user.personenTypFilter, (m, k) => m.Personentypzuordnungens.Any(i => i.Personentyp.Bezeichnung.Contains(k)), true) (Works if this is the only FilterByItems)
        //Merkmale
        //Fachtypen
        .FilterByItems(user.fachtypenFilter, (m, k) => m.Fachtypzuordnungens.Any(i => i.Fachtyp.Bezeichnung.Contains(k)), true) // If this one is not set in the frontend, the whole filter dont work anymore
        .Select(p => new
        {
            personId = p.PersonId,
            nachname = p.Nachname,
            vorname = p.Vorname,
            plz = p.Plz,
            firmBez = p.Firmenbezeichnung,
            ort = p.Ort,
            personentyp = p.Personentypzuordnungens.Select(i => new
            {
                personentypId = i.PersonentypId,
            }),
            aktuellePosition = p.AktuellePosition,
            taetigkeit = p.Tätigkeit,
            kernkompetenzen = p.Kernkompetenzen,
            datenReviewedZeitpunkt = p.DatenReviewedZeitpunkt,
        }).ToListAsync());
}

It seems like i need to make it part by part and not like i did but i dont know how i should do this correctly.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I would do something like this. I didn’t refactor your entire query but I give you the idea on how to do it:

 [HttpPost("filter/")]
    public async Task<IActionResult> Filter([FromBody] Filter user)
    {
        var baseQuery = _context.Personens;
        if (!string.IsNullOrEmpty(user.vorname))
            baseQuery  = baseQuery.Where(p => p.Vorname.ToLower().Contains(user.vorname));
        if (!string.IsNullOrEmpty(user.nachname))
            baseQuery = baseQuery.Where(p => p.Nachname.ToLower().Contains(user.nachname));
        if (!string.IsNullOrEmpty(user.anrede))
            baseQuery  = baseQuery .Where(p => p.Anrede.ToLower().Contains(user.anrede))
....

var result = await (baseQuery.Select(p => new
            {
                personId = p.PersonId,
                nachname = p.Nachname,
                vorname = p.Vorname,
                plz = p.Plz,
                firmBez = p.Firmenbezeichnung,
                ort = p.Ort,
                personentyp = p.Personentypzuordnungens.Select(i => new
                {
                    personentypId = i.PersonentypId,
                }),
                aktuellePosition = p.AktuellePosition,
                taetigkeit = p.Tätigkeit,
                kernkompetenzen = p.Kernkompetenzen,
                datenReviewedZeitpunkt = p.DatenReviewedZeitpunkt,
            }).ToListAsync());
        return Ok(result)
            }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading