wegelagerer
wegelagerer

Reputation: 3720

Filtering LINQ query

I have problem that is bothering me last couple of days.

I need to filter LINQ query using comboboxes and textboxes. The problem is I can't manage to get the result and I always get the empty gridview (used for showing filtered data).

Can anyone help me why am I getting no results at all? I've checked the debugger and the data sent to query is valid, although, I'm not sure about "string.Empty" value.

Here is the code:

string naziv, nazivEn, adresa, tel, fax, mob, email, web, oib, tip, mjesto;


        if (chkMjesto.Checked == true)
        {   
                    mjesto = cbMjesto.SelectedItem.Text;
        }
        else
            {
            mjesto = string.Empty;
            }

        if (chkTip.Checked == true)
        {
            tip = cbTip.SelectedItem.Text;
        }
        else
        {
            tip = string.Empty;
        }

        string tema;

        if (chkTema.Checked == true)
        {
            tema = cbTema.SelectedItem.Text;
        }
        else
        {
            tema = string.Empty;
        }

        if (chkAdresa.Checked == true)
        {
            adresa = txtAdresa.Text;
        }
        else
        {
            adresa = string.Empty;
        }

        if (chkTelefon.Checked == true)
        {
            tel = txtTelefon.Text;
        }
        else
        {
            tel = string.Empty;
        }

        if (chkMobitel.Checked == true)
        {
            mob = txtMobitel.Text;
        }
        else
        {
            mob = string.Empty;
        }

        if (chkEmail.Checked == true)
        {
            email = txtEmail.Text;
        }
        else
        {
            email = string.Empty;
        }

        if (chkWeb.Checked == true)
        {
            web = txtWeb.Text;
        }
        else
        {
            web = string.Empty;
        }

        if (chkOIB.Checked == true)
        {
            oib = txtOIB.Text;
        }
        else
        {
            oib = string.Empty;
        }

        if (chkNaziv.Checked == true)
        {
            naziv = txtNaziv.Text;
        }
        else
        {
            naziv = string.Empty;
        }

        if (chkNazivEn.Checked == true)
        {
            nazivEn = txtNazivEn.Text;
        }
        else
        {
            nazivEn = string.Empty;
        }

        if (chkFax.Checked == true)
        {
            fax = txtFax.Text;
        }
        else
        {
            fax = string.Empty;
        }          


        if (rblOrganizator.SelectedItem.Value == "Nije")
        {
            var pretraga = from t in db.tijeloes
                           where t.tijelo_naziv.Contains(naziv) && t.tijelo_adresa.Contains(adresa) && t.tip_tijela.tip_tijela_naziv.Contains(tip) && t.mjesto.mjesto_naziv.Contains(mjesto) 
                           where t.tijelo_telefon.Contains(tel) && t.tijelo_fax.Contains(fax) && t.tijelo_email.Contains(email) && t.tijelo_mob.Contains(mob) && t.tijelo_web.Contains(web) && t.tijelo_oib.Contains(oib) && t.tijelo_naziv_en.Contains(nazivEn)
                           select new { t.tijelo_naziv, t.tijelo_oib,t.tip_tijela.tip_tijela_naziv,t.tijelo_adresa,t.mjesto.mjesto_naziv, t.mjesto.zupanija_drzava.zupanija_naziv};

            gvTijelo.DataSource = pretraga;
            gvTijelo.DataBind();

            if (pretraga.Count() != 0)
            {
                gvTijelo.HeaderRow.Cells[0].Text = "Naziv";
                gvTijelo.HeaderRow.Cells[1].Text = "OIB";
                gvTijelo.HeaderRow.Cells[2].Text = "Tip tijela";
                gvTijelo.HeaderRow.Cells[3].Text = "Adresa";
                gvTijelo.HeaderRow.Cells[4].Text = "Mjesto";
                gvTijelo.HeaderRow.Cells[5].Text = "Regionalni centar";
            }

        }

Upvotes: 0

Views: 416

Answers (2)

Purplegoldfish
Purplegoldfish

Reputation: 5294

If I were you I would declare all the variables at the top seperately like

string naziv = string.empty;
string whatever = string.empty;  etc

Doing this will make your code smaller because you can get rid of the else statements as the variables are already set.

Sorry not a solution but might thin down the code for you a little :)

Upvotes: 0

ventaur
ventaur

Reputation: 1851

The string.Empty seems to the culprit. I would bet the SQL being generated is checking if a field contains '', which just won't likely work with data in that field.

Your best bet is to start the query before your conditionals, then append a where to it if the corresponding check box is checked.

var pretraga = db.tijeloes;
if (chkMjesto.Checked == true)
{   
    pretraga = pretraga.Where(t => t.tijelo_naziv.Contains(cbMjesto.SelectedItem.Text));
}
if (chkTip.Checked == true)
{
    pretraga = pretraga.Where(t => t.tip_tijela.tip_tijela_naziv.Contains(cbTip.SelectedItem.Text));
}
...

pretraga = pretraga.Select(t => new { t.tijelo_naziv, t.tijelo_oib,t.tip_tijela.tip_tijela_naziv,t.tijelo_adresa,t.mjesto.mjesto_naziv, t.mjesto.zupanija_drzava.zupanija_naziv });
// Bind to pretraga.
...

Upvotes: 2

Related Questions