Search code examples
c#entity-frameworklinqlinq-to-entities

How to compare a item.date in a collection to a date


The query below:

IEnumerable<activity> activityList = PortalContext.activity.Where(item => 
item.schakeling_id == newActivity.schakeling_id && 
item.startdt.Date == DateTime.Now.Date);

I am getting this error:

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

I have tried multiple different ways but can't get it working. Any help would be appreciated.


Solution

  • Create a date variable outside of your query:

    var currentdate=DateTime.Now.Date;
    
    var activityList = PortalContext.activity.Where(item => 
                       item.schakeling_id == newActivity.schakeling_id 
                       && item.startdt.Date == currentdate);