Entity framework 6.2.0 + Find

Asked

Viewed 48 times

1

Hello, I’m using a search method with Entity framework and oracle, and found 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’s running the search with the date and time. If I put Data_Intencao.Date the following error is presented:

'The specified type Member 'Date' is not supported in LINQ to Entities. Only initializers, Entity Members, and Entity navigation properties are supported.'

  • https://answall.com/questions/14857/erro-ao-passar-adddays-em-express%C3%A3o-lambda/14862#14862 Read

  • other link https://answall.com/questions/176823/filtro-para-os-pr%C3%B3ximos-90-dias-Linq-Asp-net-mvc/176832#176832

  • in its specific case: https://answall.com/a/176832/54880

2 answers

1


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

Where(x => DbFunctions.TruncateTime(x.Data_Intencao) >= Periodo.Date && c => c.SubOrigem_EF);
  • I did it this way: var test = Find(x => Dbfunctions.Truncatetime(x.Data_intencao) >= Data_inicio.Value.Date, c => c.Suborigem_ef); and I got the same error

  • That field Data_Inicio is from the database? In the question does not have this field, it has the Periodo.

  • is a variable, any date!

  • Try changing to Where

  • Dbfunctions.Truncatetime(data)! So it works, it went wrong the first time because I did not use this method in the PERIODO variable. Thank you

0

You can simply reset the time of the Periodo.Date cut-off 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);
  • The problem is that this time is already zero. The problem is that I want to filter only with the days 01 and the end of the month regardless of the time

  • The code you put in the example expresses the filter of a date equal or larger, I could not understand what you want to do. Could pass an example of what would come in the Data_intencao and what would come in the Period.Date and what result awaits?

Browser other questions tagged

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