c# - Dynamic EF Where Clause raising ArgumentNullException -
i'm trying code method that, in it's class given values of of attributes, returns filtered dbset. code, far, is:
public ienumerable<pesquisa> pesquisas { { prometheusdbcontext db = new prometheusdbcontext(); var temp = db.pesquisas; if ((this.filtro.nome != null) && (this.filtro.nome.trim() != "")) { temp = (temp.where(p => sqlfunctions.patindex(this.filtro.nome, p.nome) > 0) dbset<pesquisa>); } if ((this.filtro.codtipopesquisa != null) && (this.filtro.codtipopesquisa.trim() != "")) { temp = (temp.where(p => p.codtipopesquisa == this.filtro.codtipopesquisa.trim()) dbset<pesquisa>); } if ((this.filtro.idstatuspesquisa != null) && (this.filtro.idstatuspesquisa > 0)) { temp = (temp.where(p => p.idstatuspesquisa == this.filtro.idstatuspesquisa) dbset<pesquisa>); } if ((this.filtro.datacriacao_inicial != null) && (this.filtro.datacriacao_final != null)) { temp = (temp.where(p => (p.datacriacao >= this.filtro.datacriacao_inicial) && (p.datacriacao <= this.filtro.datacriacao_final)) dbset<pesquisa>); } else { if (this.filtro.datacriacao_inicial != null) { temp = (temp.where(p => p.datacriacao >= this.filtro.datacriacao_inicial) dbset<pesquisa>); } if (this.filtro.datacriacao_final != null) { temp = (temp.where(p => p.datacriacao <= this.filtro.datacriacao_final) dbset<pesquisa>); } } return temp .include(p => p.usuario) .include(p => p.statuspesquisa) .include(p => p.tipopesquisa) .include(p => p.modelotermoadesao) .include(p => p.pacientes) .tolist(); }
problem is: everytime 1 of attributes filled value (i.e.: this.filtro.nome = "test" ), tolist() raises argumentnullexcpetion. ideas?
you shouldn't cast dbset @ end of each line. also, declare
iqueryable<pesquisa> temp = db.pesuisas; // code follows.
the reason behind although start dbset, applying operators changes type. dynamic cast returns null then.
Comments
Post a Comment