Where with date and time - Entity Framework

0

I'm doing a select with entity framework. where I need to use the where, with date and time, in SQL SERVER select, I would do it this way:

     select dbo.contas_receber.id, dbo.contas_receber.tipo_conta,dbo.contas_receber.data_pagamento, dbo.contas_receber.data_pagamento,dbo.contas_receber.observacao, dbo.contas_receber.valor,dbo.contas_receber.forma_pagamento from dbo.contas_receber  where (dbo.contas_receber.data_pagamento) >= ('" + data_abertura + "')  and DATEADD(day,  -DATEDIFF(day, 0,'" + data_abertura + "'), dbo.contas_receber.data_pagamento) >= '" + hora_aber + "'

In the entity, I could only do the part of the date, I can not do the time part:

_context.ContasApagar.Select(p => new CaixasViewModel
        {
            Data = p.DataPagamento,
            Hora = p.DataPagamento,
            Historico = p.Obs,
            Valor = p.ValorPago,
            Forma = p.FormaPagamento
        }).ToList()).Where(p => p.Data >= data_abertura && p.Hora >= DateTime.Parse(hora_aber));

If I do this, it does not do the correct select, it filters by date greater than date_abertura, and time greater than opening_time, but if the time is smaller, but it is a day ahead of date_abertura, it should appear in select .

    
asked by anonymous 13.07.2018 / 13:55

1 answer

1

I see no reason to separate DateTime ... in C # DateTime treats both, so if you convert "08:10" to DateTime, the value will be 01/01/0001 08:10:00. p>

You need to inform Data as well.

In addition, you can filter objects before the Select, so you would filter in p.DataPayment.

DateTime hora_abertura = DateTime.Parse(hora_aber);
DateTime data = data_abertura.AddHours(hora_abertura.Hour).AddMinutes(hora_abertura.Minute);


_context.ContasApagar.Where(p=> p.DataPagamento >= data).Select(p => new CaixasViewModel
        {
            Data = p.DataPagamento,
            Hora = p.DataPagamento,
            Historico = p.Obs,
            Valor = p.ValorPago,
            Forma = p.FormaPagamento
        }).ToList();

The DateTime conversion, I put in .NETFiddle

    
13.07.2018 / 14:13