0

I am getting an error: The specified type member 'Date' is not supported in LINQ to Entities.

I have a code where I have tried to follow the answers of other two other posts: LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method and Linq To Sql compare Time only

Also this method is executed by many threads in parallel.

public List<Element> GetResults()
{
    List<Element> elements = new List<Element>();

   try {

    using (DbContext context = new EntitiesContext())
    {

    DateTime dateTimeNow = DateTime.Now;
    TimeSpan timeSpanNow = DateTime.Now.TimeSpan;      
    elements = context.Elements.
                          // Some other Includes
                          //Element has a field System.DateTime DateTime
                          .Include(_ => _.DateTime)              
                          .Where(o => DbFunctions.TruncateDate(o.DateTime.Date) == dateTimeNow 
&& DbFunctions.CreateTime(o.DateTime.Hour, o.DateTime.Minute, o.DateTime.Second) <= timeSpanNow))
.ToList();


    }
}
catch(Exception ex)
{
    //logging
} 

return elements;
    }
Tito
  • 722
  • 4
  • 26
  • 55

1 Answers1

1

First, there is no such thing as DbFunctions.TruncateDate, as far as I know. I assume that's just a typo and you meant DbFuctions.TruncateTime.

Then, you are using DbFunctions.TruncateTime because EF doesn't understand how to translate someDateTime.Date to SQL. By doing

DbFunctions.TruncateDate(o.DateTime.Date)

you are still using the same thing which EF doesn't understand, so you need to get rid of that Date:

DbFunctions.TruncateDate(o.DateTime)
Evk
  • 98,527
  • 8
  • 141
  • 191