简体   繁体   中英

How can i cast IQueryable<> query to IQueryable<obj.getType()>?

How can i factorize this code? my filter returns several type of document. the difficult is that i have some query per type... i would like a generic method to return a correct query

thanks

        if(this.comboBoxType.Text.Equals("Vente"))
        {
            IQueryable<Vente> queryV = ContexteDAO.ContexteDonnees.Vente
               .Include("Client")
               .Include("Paiement")
               .Include("Employe").OrderBy(v => v.venteID);

            if (this.tbxNomCli.Text != "")
                queryV = queryV.Where(v => v.Client.nom.Contains(this.tbxNomCli.Text));
            if (this.comboBoxEtat.Text != "Tous")
                queryV = queryV.Where(v => v.etat == this.comboBoxEtat.Text);
            if (this.checkBoxDate.Checked)
                queryV = queryV.Where(v => v.date.Equals(this.dateTimePicker.Value.Date));
            if (this.tbxTva.Text != "")
                queryV = queryV.Where(v => v.Client.numEntreprise.Contains(this.tbxTva.Text));
            if (this.checkBoxVendeur.Checked)
            {
                Employe employe = this.comboBoxVendeur.SelectedItem as Employe;
                queryV = queryV.Where(v => v.Employe.login.Equals(employe.login));
            }

            this.documentBindingSource.DataSource = queryV.ToList();
        }
        if (this.comboBoxType.Text.Equals("Commande"))
        {
            IQueryable<Commande> queryC = ContexteDAO.ContexteDonnees.Commande
               .Include("Client")
               .Include("Paiement")
               .Include("Employe").OrderBy(c => c.commandeID);

            if (this.tbxNomCli.Text != "")
                queryC = queryC.Where(v => v.Client.nom.Contains(this.tbxNomCli.Text));
            if (this.comboBoxEtat.Text != "Tous")
                queryC = queryC.Where(v => v.etat == this.comboBoxEtat.Text);
            if (this.checkBoxDate.Checked)
                queryC = queryC.Where(v => v.date.Equals(this.dateTimePicker.Value.Date));
            if (this.tbxTva.Text != "")
                queryC = queryC.Where(v => v.Client.numEntreprise.Contains(this.tbxTva.Text));
            if (this.checkBoxVendeur.Checked)
            {
                Employe employe = this.comboBoxVendeur.SelectedItem as Employe;
                queryC = queryC.Where(v => v.Employe.login.Equals(employe.login));
            }

            this.documentBindingSource.DataSource = queryC.ToList();
        }

You could make a generic approach:

public IQueryable<T> GetData<T>( string identifier )
{
     switch( identifier )
     {
         case "Vente":
         {
             return ContexteDAO.ContexteDonnees.Vente
                                               .Include("Client")
                                               .Include("Paiement")
                                               .Include("Employe")
                                               .OrderBy(v => v.venteID);
             // do more stuff

             break;
         }

         // add more cases

         default:
         {
             return null;
         }
     }
}

the call would look like:

IQueryable<Vente> result = GetData<Vente>( "Vente" );

it would solve your problem but i won't like it, because you need to specify the type AND need an identifier which selection you would like to perform. This could lead to an exception really fast when you have something like GetData<Vente>( "OtherEntity" ) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM