Complex query when passing parameters

Asked

Viewed 1,223 times

2

I need to make a query, but I’m having a hard time. I have 5 filters on the page. Three of them come from three dropdown (select). I can only consider select if the text in them is different from "NO FILTER", first step. The last select, has only two lines, being the first GENERAL and the second SOUZA CRUZ. If it is general, it should bring everything that is in the field visao, but only that which is souza cruz. As I can not give an if inside a Burst, I get this difficulty. I have this consultation, that a colleague passed me here in this forum, in this way, but I find it difficult to put more filters in this new context. The appointment Morrison gave me was this and attended to what I requested.

.Where(cn => (cn.x1.a1.o2.NumOs == Convert.ToInt32(_osparceiro)) && (_cnpj == "" || cn.cnpj == _cnpj))

Now I’m having trouble making mine. See my method:

[HttpPost]
        public JsonResult MontaResultadoPdv(string _cnpj, string _contato, string _filtrarrede, string _filtarstatus, string _visao)
        {
            V99_WEBEntities db = new V99_WEBEntities();

            var resultadopdv = (from pdv in db.T_PDV
                                    .Where(res => (res.CNPJ == "" || res.CNPJ == _cnpj))
                                select new { 
                                            pdv.RazaoSocial,
                                            pdv.}).ToList(); 

            return Json(new {  }, JsonRequestBehavior.AllowGet);
        }

These are the parameters that come from the selects:

string _filtrarrede, string _filtarstatus, string _visao

If you see null or empty, for example, the query will give me an unexpected result.

1 answer

3


If I understand correctly, maybe this will help you:

var resultadopdv = (from pdv in db.T_PDV
            .Where(res => (res.CNPJ == "" || res.CNPJ == _cnpj))
        select new { 
                    pdv.RazaoSocial,
                    pdv.});

if(!string.IsNullOrEmpty(_filtrarrede))
    resultadopdv = resultadopdv.Where(e => e.rede == _filtrarrede);
if(!string.IsNullOrEmpty(_filtarstatus))
    resultadopdv = resultadopdv.Where(e => e.status == _filtarstatus);
if(!string.IsNullOrEmpty(_visao))
    resultadopdv = resultadopdv.Where(e => e.visao == _visao);

return Json(resultadopdv.ToList(), JsonRequestBehavior.AllowGet);
  • Let me understand. I can make the conditions(Where) out of the expression, ie as if it were another expression. cool this, I will test and I already close the post, in case everything goes well.

  • 2

    Remembering an important thing. The query is not executed before calling the method ToList(). Soon you can prepare the entire query based on the parameters you received without harming the performance.

  • @pnet, forgot to assign the result of Where for the variable. Take a look at the edition.

  • Perfect ... putting together the answer and the explanation @Reiksiel would give the final answer!

Browser other questions tagged

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