简体   繁体   English

我如何将IQueryable &lt;&gt;查询转换为IQueryable <obj.getType()> ?

[英]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" ) . 当您拥有诸如GetData<Vente>( "OtherEntity" )类的东西时,这可能会很快导致异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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