Home > Mobile >  Multiple clauses blocking the whole filter
Multiple clauses blocking the whole filter

Time:11-18

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.

CodePudding user response:

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.AsQueryable();
        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)
            }
  • Related