1
I have a method which returns records from the database filtered between dates, and is built as follows:
public List<ProcessosInformacoesAnexos_Model> DevolveTodosAnexos(DateTime dataInicial, DateTime dataFinal, int pagina, int numeroRegistos, out int totalRegistos)
{
var query = from t1 in baseDados.tbProcessos_Informacoes_Anexos
select t1;
//Data
if(dataInicial!= DateTime.MinValue && dataFinal!=DateTime.MinValue)
{
var dataInicial2 = DateTime.Parse(dataInicial.ToString("yyyy/MM/dd HH:mm:ss"));
var dataFinal2 = DateTime.Parse(dataFinal.ToString("yyyy/MM/dd HH:mm:ss"));
query = query.Where(x => x.tbProcessos.DataCriacao >= dataInicial2 && x.tbProcessos.DataCriacao <= dataFinal2);
}
totalRegistos = query.Count();
pagina--;
var listaProcesso = query.OrderByDescending(x=>x.DataCriacao).Skip(pagina * numeroRegistos).Take(numeroRegistos).ToList();
var resultado = (from tbProcessosAnexos in listaProcesso
select new ProcessosInformacoesAnexos_Model()
{
Id = tbProcessosAnexos.Id,
ProcessoId = tbProcessosAnexos.ProcessoId,
IdProcessoInformacao = tbProcessosAnexos.IdProcessoInformacao,
Ficheiro = tbProcessosAnexos.Ficheiro,
IdTipoAnexo = tbProcessosAnexos.IdTipoAnexo,
Designacao = tbProcessosAnexos.Designacao,
Descricao = tbProcessosAnexos.Descricao,
UtilizadorCriacao = tbProcessosAnexos.UtilizadorCriacao,
DataCriacao = tbProcessosAnexos.DataCriacao,
}).ToList();
return resultado;
}
The problem occurs when I apply the filters in the Line expression inside the if where, when picking the query that is built, it puts the dates as follows: @p__linq__0 and @p__linq__1, below is the query that results from the Line.
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[ProcessoId] AS [ProcessoId],
[Extent1].[IdProcessoInformacao] AS [IdProcessoInformacao],
[Extent1].[Ficheiro] AS [Ficheiro],
[Extent1].[IdTipoAnexo] AS [IdTipoAnexo],
[Extent1].[Designacao] AS [Designacao],
[Extent1].[Descricao] AS [Descricao],
[Extent1].[Custos] AS [Custos],
[Extent1].[CustosValor] AS [CustosValor],
[Extent1].[CustosFicheiro] AS [CustosFicheiro],
[Extent1].[CustosData] AS [CustosData],
[Extent1].[Pago] AS [Pago],
[Extent1].[PagoDocumento] AS [PagoDocumento],
[Extent1].[PagoData] AS [PagoData],
[Extent1].[IdMotivoRecibo] AS [IdMotivoRecibo],
[Extent1].[Ordem] AS [Ordem],
[Extent1].[UtilizadorCriacao] AS [UtilizadorCriacao],
[Extent1].[DataCriacao] AS [DataCriacao]
FROM [dbo].[tbProcessos_Informacoes_Anexos] AS [Extent1]
INNER JOIN [dbo].[tbProcessos] AS [Extent2] ON [Extent1].[ProcessoId] = [Extent2].[Id]
WHERE ([Extent1].[Custos] = 1) AND ([Extent2].[DataCriacao] >= @p__linq__0) AND ([Extent2].[DataCriacao] <= @p__linq__1)
Does anyone know what it might be?
EDIT: After all I was able to figure out a way, whether it was to put this filter between dates just when I ask the query:
var query = from t1 in baseDados.tbProcessos_Informacoes_Anexos
where (t1.DataCriacao>= dataInicial && t1.DataCriacao <= dataFinal)
select t1;
It was necessary to put a "Where", but I do not understand well why :/
pq take the date, convert to string, and then call Parse by converting to Datetime again ? And what’s the problem with the query ?
– Rovann Linhalis
@Rovannlinhalis I do this because it comes from a different format (dd-mm-yyyy) of the view, so I had to put it in this format :/
– Araújo
is coming as
DateTime
, no matter the format– Rovann Linhalis