Consult in several fields of the record in a single query

Asked

Viewed 455 times

3

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

Someone’s been through it?

Code I’m using to meet 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;
  • 3

    You need to post your code with the problem/doubt if it doesn’t get hard to help.

  • You will use Angular in Views also?

  • I will post the code yes @Dontvotemedown

  • @Ciganomorrisonmendez, at first I will not use Angular

  • See if this helps you: http://answall.com/a/60613/20615

  • Thank you @Randrade!

Show 1 more comment

1 answer

2


It’s correct your approach, but I’d 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();
  • Thanks @Ciganomorrisonmendez, I thought I would have some more abstract way to effect this, but it fits me perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.