我怎样才能铸造IQueryable<>;查询到IQueryable<;obj.getType()>;
本文关键字:gt IQueryable lt obj getType 查询 | 更新日期: 2023-09-27 18:27:03
如何分解此代码?我的过滤器返回几种类型的文档。困难的是,我对每个类型都有一些查询。。。我想要一个返回正确查询的通用方法
感谢
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();
}
您可以使用一种通用方法:
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" )
的东西时,这可能会很快导致异常。