LINQ to SQL query not returning correct DateTime LINQ to SQL query not returning correct DateTime sqlite sqlite

LINQ to SQL query not returning correct DateTime


Change

public DateTime synctime{ get; set; }

to:

public DateTime? synctime { get; set; }

Hope it will helps. What happens is DateTime is NOT NULL data type and it enforces to put a default value there (01/01/0001) to make sure that non-null date will be submitted.Don't know whether it can be an issue..try and check


I looked at all possible scenarios when this result can happen. There is only one logical explanation to your problem. Your

db.Table<SyncAudit>()

is an empty collection and when you select

db.Table<SyncAudit>()                   .OrderByDescending(c => c.SyncTime)                   .Select(c => c.SyncTime)                   .FirstOrDefault();

it will naturally return you default value of the DateTime.Check your database connection and make sure it works first.


To diagnose what the problem is, go back to basics. Change the LINQ into a foreach loop to iterate over all the records, and find the biggest value. Now step through the code (because I suspect that won't do what you expect either) and you'll see what's going wrong. Then tell us what was wrong.