I need to return a list of items from my database that expire at a pre-specified time on the date supplied by the item. My erroneous code is as follows:
return All().Where(o => new DateTime(o.expiry_date.Year, o.expiry_date.Month, o.expiry_date.Day, 17, 30, 0) >= DateTime.Now)
The error I get is:
Only parameterless constructors and initializers are supported in LINQ to Entities
Does anyone know how I can fix this?
TruncateTime(Nullable<DateTime>) When used as part of a LINQ to Entities query, this method invokes the canonical TruncateTime EDM function to return the given date with the time portion cleared.
Entity Framework is an object-relational mapping (ORM) framework for connecting C# code to external databases, usually SQL Server. LINQ is a query language embedded into C# and a set of extension methods in order to make it useful.
Use EntityFunctions instead. Maybe CreateDateTime method.
So maybe like this:
return All().Where(o => EntityFunctions.CreateDateTime(o.expiry_date.Year, o.expiry_date.Month, o.expiry_date.Day, 17, 30, 0) >= DateTime.Now)
Update:
When using EF6, use DbFunctions instead.
You can use:
var result = await db.Articles
.Where(TruncateTime(x.DateCreated) >= EntityFunctions.TruncateTime(DateTime.Now))
.ToListAsync();
Please use:
return All().Where(o => EntityFunctions.CreateDateTime(o.expiry_date.Year, o.expiry_date.Month, o.expiry_date.Day, 17, 30, 0) >= DateTime.Now)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With