c# - how to query nullable datetime in linq -
i calling following query, display latest date fieldname 'uploaddate', experiencing exception error such as:
the entity or complex type 'cdwmodel.database_bd' cannot constructed in linq entities query.","exceptiontype":"system.notsupportedexception","stacktrace":
code:
public ienumerable<database_bd> getdate() { var data = (from c in db.database_bd select new database_bd() { uploaddate = c.uploaddate }).tolist().orderbydescending(c => c.uploaddate).take(1); return data; }
database_bd model class:
public partial class database_bd { public nullable<system.datetime> uploaddate { get; set; } }
working solution:
public datetime? getdate() { return data = db.database_bd.select(d => d.uploaddate) .orderbydescending(c => c) .firstordefault(); }
if want display latest date uploaddate
, dont need create new database object.
in example data single date value, or null if there no records:
var data = db.database_bd.select(d => d.uploaddate) .orderbydescending(c => c) .firstordefault();
if need return database_bd
object, you'd remove select changes output:
public database_bd getdate() { var data = db.database_bd.orderbydescending(c => c.uploaddate) .firstordefault(a => a.uploaddate.hasvalue); return data; }
that give newest database_bd
object in table.
Comments
Post a Comment