Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Date Parsing

I am trying to do a LINQ to Entities query (am still very new with EF and am learning where it is diff than LinqToSQL) and am trying to do something like this:

DateTime begin = new DateTime(2011, 1, 1);

Apps = (from app in context.Instances
                 where app.ReleaseDate.Date == begin
                 select new ScalpApp
                 {
                 Image = app.Image,
                 PublisherName = app.PublisherName,
                 }).ToList();

while this works in LinqPad, it doesn't in my code. The thrown exception is:

The specified type member 'Date' is not supported in LINQ to Entities.

Only initializers, entity members, and entity navigation properties are supported.

How do I do this in LinqToEF? The field in the DB is a full DateTime (with timecode) and I am trying to parse based on date only.

like image 523
Brandon Watson Avatar asked Jan 20 '23 18:01

Brandon Watson


1 Answers

You cann't simple use 'app.ReleaseDate.Data', but you can:

var begin = new DateTime(2011, 1, 1);
var end = begin.AddHours(24);

Apps = (from app in context.Instances
             where 
               (app.ReleaseDate >= begin) and
               (app.ReleaseDate < end)
             select new ScalpApp
             {
             Image = app.Image,
             PublisherName = app.PublisherName,
             }).ToList();
like image 149
Andrew Orsich Avatar answered Feb 01 '23 11:02

Andrew Orsich