Reputation: 523
I'm using LINQ expressions to filter what data I want to recieve from database. Here is my expression:
var ignicoes = _context.Ignicoes
.Where(e => (e.Estado == Ignicoes.EstadoIgnicao.emAvaliacao && (e.DataInicioPropostaIgnicao - dataAtual).TotalHours <= 12 ) ||
e.Estado == Ignicoes.EstadoIgnicao.aceite ||
(e.Estado == Ignicoes.EstadoIgnicao.concluido && (e.DataDecisaoIgnicao - dataAtual).TotalHours <= 12 ))
.Include(i => i.ListaOcorrencias);
DataInicioPropostaIgnicao - is the date when it was posted to the database dataAtual- is the current date of the system
I have class called Ignicoes, that has a property called Estado. Dependig on the Estado and how many hours the "Ignicao" has been posted to the database. I only want to return data that has been twelve hours or less in the database. Why am I having an error with this expression? Here is the error:
I don't know if I made myself clear, but I hope someone can help me with this issue. Thank you
Upvotes: 0
Views: 193
Reputation: 2565
When using EntityFramework you have to pay attention on how the Linq query is written: the query builder can't always translate the linq query to sql.
Based on this article https://entityframework.net/subtract-two-dates if you are using SQLServer the following should work:
int noOfHours = 12;
DateTime oldDate = DateTime.Now.Subtract(new TimeSpan(noOfHours, 0, 0, 0));
var ignicoes = _context.Ignicoes
.Where(e => (e.Estado == Ignicoes.EstadoIgnicao.emAvaliacao && e.DataInicioPropostaIgnicao >= oldDate ) ||
e.Estado == Ignicoes.EstadoIgnicao.aceite ||
(e.Estado == Ignicoes.EstadoIgnicao.concluido && e.DataDecisaoIgnicao >= oldDate ))
.Include(i => i.ListaOcorrencias);
Upvotes: 1