3
Well I’d like to know first what the difference is between Func<T, bool>
for Expression<Func<T, bool>>
And how do I convert from Func<T, bool>
for Expression<Func<T, bool>>
if it is possible.
Or I need all my code to be Expression instead of Func. But I don’t know how to Convert and concatenate Expression. In the Filter method().
public class MovimentoTratamento : Tratamento
{
public string Nr_Nota_Fiscal { get; set; }
public string Dt_Emissao_Nota_Fiscal { get; set; }
}
public abstract class Tratamento
{
[Key]
public int ID{ get; set; }
public string Cd_Sap_Distribuidor_Matriz { get; set; }
public string Cd_Sap_Distribuidor_Filial { get; set; }
public DateTime Dt_Criacao { get; set; }
public int Id_Log { get; set; }
}
private Func<Tratamento, bool> FiltroPadrao(FiltroViewModel filtroViewModel)
{
DateTime inicial, final;
DateTime.TryParse(filtroViewModel.CargaInicial, out inicial);
DateTime.TryParse(filtroViewModel.CargaFinal, out final);
Func<Tratamento, bool> f = x =>
((final == DateTime.MinValue) || (x.Dt_Criacao >= inicial && x.Dt_Criacao <= final.AddDays(1)))
&&
(string.IsNullOrEmpty(filtroViewModel.CnpjMatriz) || x.Cd_Sap_Distribuidor_Matriz == filtroViewModel.CnpjMatriz)
&&
(string.IsNullOrEmpty(filtroViewModel.CnpjFilial) || x.Cd_Sap_Distribuidor_Filial == filtroViewModel.CnpjFilial)
&&
(filtroViewModel.TipoErro == "0" || x.Cd_Erro == filtroViewModel.TipoErro)
&&
(
(filtroViewModel.StatusSelected == 3) ||
(filtroViewModel.StatusSelected == 1 && x.Id_Log == 0) ||
(filtroViewModel.StatusSelected == 2 && x.Id_Log > 0)
);
return f;
}
private Func<MovimentoTratamento, bool> FiltroNotaFiscal(FiltroViewModel filtroViewModel)
{
return x => (string.IsNullOrEmpty(filtroViewModel.NotaFiscal) || x.Nr_Nota_Fiscal == filtroViewModel.NotaFiscal);
}
public IQueryable<MovimentoTratamento> Filter()
{
Func<Tratamento, bool> filter = FiltroPadrao(vm);
Func<MovimentoTratamento, bool> mov = FiltroNotaFiscal(vm);
UnirExpressionMovimento(filter, mov);
return repo.Get(f);
}
private Func<MovimentoTratamento, bool> UnirExpressionMovimento(Func<Tratamento, bool> filter, Func<MovimentoTratamento, bool> mov)
{
Func<MovimentoTratamento, bool> f = value => filter((Tratamento)value);
return value => f(value) && mov(value);
}
related, http://answall.com/questions/2822/o-que-s%C3%A3o-lambda-Expressions-e-qual-a-sacada-em-us%C3%A1-las/2834#2834
– Vinícius
@Diegozanardo, as much as I’ve tried to help you with your problem, I disagree with your architecture, I don’t think it’s a good idea to implemenar
Repository Pattern
additional on theEntity Framework
, you can read more about it in When to use Entity Framework with Repository Pattern?– Tobias Mesquita
still addressing this subject, avoid creating something very complex, it would be much simpler if you perform this direct query on your
DataSet
, without the need to create generic functions... although it is not very connected to your problem, see this article/video: Stack Overflow and Stack Exchange Architecture– Tobias Mesquita