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 に答える