c# – Query in multiple fields of the record in a single query

Question:

I'm developing an item list filter, and I'd like to filter by any terms in the items record. I've seen this working on Angular, but I'm developing for ASP.NET MVC 5 and using Entity Framework + Linq for queries.

Has anyone ever experienced this?

Code that I'm using to meet the demand:

List<Chamado> chamados = (from e in db.Chamado where e.StatusChamado != true select e).ToList();
        if(filtro != null)
        {
            chamados = chamados.Where(s => s.Id.ToString().Contains(filtro)
                                                       || s.Assunto.Contains(filtro)
                                                       || s.Descricao.Contains(filtro)
                                                       || s.ObraDestino.Descricao.Contains(filtro)
                                                       || s.ResponsavelChamado.Nome.Contains(filtro)).ToList();
        }
        return chamados;

Answer:

Your approach is correct, but I would do it differently:

    var chamadosQuery = db.Chamado.Where(!e.StatusChamado);

    if(filtro != null)
    {
        chamadosQuery = chamadosQuery.Where(s => s.Id.ToString().Contains(filtro)
                                                   || s.Assunto.Contains(filtro)
                                                   || s.Descricao.Contains(filtro)
                                                   || s.ObraDestino.Descricao.Contains(filtro)
                                                   || s.ResponsavelChamado.Nome.Contains(filtro));
    }

    return chamados.ToList();
Scroll to Top