Query in several fields of the record in a single query

3

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

Has anyone ever been through this?

Code that I am 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;
    
asked by anonymous 11.09.2015 / 15:34

1 answer

2

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();
    
11.09.2015 / 16:22