c# - How can i cast IQueryable<> query to IQueryable<obj.getType()>? -
how can factorize code? filter returns several type of document. difficult have query per type... generic method return 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 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 employe; queryc = queryc.where(v => v.employe.login.equals(employe.login)); } this.documentbindingsource.datasource = queryc.tolist(); }
you make 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); // more stuff break; } // add more cases default: { return null; } } }
the call like:
iqueryable<vente> result = getdata<vente>( "vente" );
it solve problem won't it, because need specify type , need identifier selection perform. lead exception fast when have getdata<vente>( "otherentity" )
.
Comments
Post a Comment