Entity framework 6.2.0 + Find

0

Hello, I'm using a search method with entity framework and oracle, and I encountered a problem with dates.

In my search I use the following logic:

Find(x => x.Data_Intencao >= Periodo.Date, c => c.SubOrigem_EF);

But in my Data_Intencao she is searching the date and time. If I put Data_Intencao.Date the following error is displayed:

  

'The specified type member' Date 'is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported. '

    
asked by anonymous 02.01.2019 / 19:03

2 answers

3

Use the DbFunctions.TruncateTime(data) method to use the date and ignore the time.

Where(x => DbFunctions.TruncateTime(x.Data_Intencao) >= Periodo.Date && c => c.SubOrigem_EF);
    
02.01.2019 / 19:26
0

You can simply zero the time of the Cut date of the Date.Date or create an extended function:

 DateTime date = new DateTime(Periodo.Date.Year, Periodo.Date.Month, Periodo.Date.Day, 0,0,0);

Find(x => x.Data_Intencao >= date, c => c.SubOrigem_EF);

Or to make it more elegant:

    public class Periodo
{

    public DateTime Date { get; private set; }

    public Periodo(DateTime date)
    {
        Date = date;
    }
}

public static class DateTimeCustom
{

    public static DateTime ToZeroHora(this DateTime source)
    {
        return source.AddHours(-source.Hour).AddMinutes(-source.Minute).AddSeconds(-source.Second);

    }
}

Find(x => x.Data_Intencao >= Periodo.Date.ToZeroHora(), c => c.SubOrigem_EF);
    
02.01.2019 / 20:15