Well, I know that's a lot of questions about, but I didn't found a answer for my problem yet.
I have rules in entity classes:
class Evento:
public virtual ICollection<Sessao> Sessao { get; set; }
public bool Ativo()
{
return DataPublicacao <= DateTime.Today
&& (!DataFim.HasValue || DataFim.Value >= DateTime.Today)
&& Sessao.Any(sessao => sessao.Ativa());
}
class Sessao:
public bool Ativa() => Status == StatusSessao.Ativa && (Recorencia && DataInicio <= DateTime.Today || (!Recorencia && DataInicio <= DateTime.Today && DataFim >= DateTime.Today));
//I had try to put DateTime.Today in a variable, but same exception
the LINQ code:
var cardsEventos = await Consulta //DbSet<Evento>
.Include(x => x.Sessao)
.Where(ev => ev.Ativo())
.Where(x => x.Destaque) //...
When call the method, it throws the exception with that message:
System.InvalidOperationException: The LINQ expression 'DbSet<Evento>()
.Where(e => e.Ativo())' could not be translated.
But if i put same rules directly on linq, it works.
All rules are same in the another parts of the software, so my think is have just one point of failure of these rules, how can I do that?
I would suggest to use LINQKit. It needs configuring
DbContextOptions:Define your methods in the following way. We just need Expression Tree from them:
Then your LINQ query should work without any changes: