Linq - filters between dates

Asked

Viewed 369 times

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 ?

  • @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 :/

  • is coming as DateTime, no matter the format

1 answer

1

Araujo.

This is because c# prevents the Sqlinjection attack, where the data you saw from the outside, in case passed by the user, can be malicious and to ensure that this does not occur, it creates these variables, where you find these parameters.

In order to use this query in a DBMS you need to declare these variables and set them with the corresponding values for search. In the code run by c# it takes care of this task, but when we get the generated query and want to use it we need to declare it above and then set its values.

DECLARE @p__linq__0 datetime
DECLARE @p__linq__1 datetime

SET @p__linq__0 = '2019-05-02'
SET @p__linq__1 = '2019-08-09'

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)

This way you arrow these variables and it starts using these values in the query.

  • Now I understand why he created this :) :/

  • I had a similar problem, in my case it was because the dates in the bank were out of time, and I passed a date with the current time, when I only took the date value without the time, he managed to make the filter. Not if it’s your case you might as well take a look.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.