0

我怎样才能分解这段代码?我的过滤器返回几种类型的文档。困难在于我对每种类型都有一些查询...我想要一个通用方法来返回正确的查询

谢谢

        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();
        }
4

1 回答 1

1

你可以做一个通用的方法:

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;
         }
     }
}

电话看起来像:

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

它会解决你的问题,但我不喜欢它,因为你需要指定类型并且需要一个标识符,你想执行哪个选择。当你有类似的东西时,这可能会很快导致异常GetData<Vente>( "OtherEntity" )

于 2013-08-10T13:56:57.220 回答