Search Filter

Asked

Viewed 808 times

2

I have an Items entity that relates to the product table and it is already in production and is working perfectly! Now I need to create a search report in which the user can determine through filters the conditions of this query. The query fields the user can choose are:

  • Date - Date Request To
  • Shop
  • Item
  • Brand
  • Forma Pagamento.

The report is very simple and the result will be displayed through a View with List. The report has already been created without the use of fields previously informed. I am using Entityframework in the queries and for this report I am currently using this query:

return Db.TabCadStatusOrdemServicoItem.ToList().OrderByDescending(x => x.Descricao);

The question is: how do I include the fields in the above query knowing that for some fields the user can select the all option (e.g. store)?

Grateful for the strength!

2 answers

2

If I understand the question correctly, you can do something like this:

var result = Db.TabCadStatusOrdemServicoItem.ToList();

//Para cada campo do filtro, adicionar um bloco If igual ao abaixo
if (!modelFiltro.Foo.IsNullOrWhiteSpace())
{
    result = result.Where(x => x.Foo.Equals(modelFiltro.Foo))
}


return result.OrderByDescending(x => x.Descricao);

Of course, if the user selects "All" in the store filter, you simply do not submit any store ID.

  • 1

    Eduardo, thank you so much for your help, I will apply your tip on my system - Thank you!

1


I think that this answer here can be useful for you.

In your case, you can make a Viewmodel whose fields are Nullable:

public class ParametrosPesquisaViewModel
{
    public DateTime? DataInicial { get; set; }
    public DateTime? DataFinal { get; set; }
    public int? LojaId { get; set; }
    public int? ItemId { get; set; }
    public int? MarcaId { get; set; }

    public IEnumerable<TabCadStatusOrdemServicoItem> Resultados { get; set; }
}

Example of predicate construction:

        var query = db.TabCadStatusOrdemServicoItem.AsQueryable();
        if (viewModel.DataInicial != null)
        {
            if (viewModel.DataFinal != null)
            {
                query = query.Where(a => a.Data >= viewModel.DataInicial && a.Data <= viewModel.DataFinal);
            }
            else
            {
                query = query.Where(a => DbFunctions.TruncateTime(a.Data) == viewModel.DataInicial);
            }
        }

        if (viewModel.LojaId != null)
        {
            query = query.Where(a => a.LojaId == (int)viewModel.LojaId);
        }

        // E assim por diante.

         return query.ToList();
  • In this case Gypsy Morrison should I use two Models in the same View (Parametrossearviewmodel + Tabcadstatusordemservicoitem) and one for the filter and the other for the listing of Items? if yes, not abusing goodwill :) - I have no idea how to implement two models in the same View.

  • Good, then. In fact you just pass to the View ParametrosPesquisaViewModel. The list of results you fill in on Controller.

  • Blz, the suggestion was applied in my project and it all worked out. Thank you guys!!!

Browser other questions tagged

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