Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL query not returning correct DateTime

I am trying to pull the most recent DateTime field from SQLite and it is returning the incorrect time.

Here's data in the database:

enter image description here

And here is my method to get the most recent DateTime via LINQ:

    public string getLastSyncTime()
    {
        using (var db = new SQLite.SQLiteConnection(this.DBPath))
        {
            var query = db.Table<SyncAudit>()
                       .OrderByDescending(c => c.SyncTime)
                       .Select(c => c.SyncTime)
                       .FirstOrDefault();

          return query.ToString();
        }
    }

The issue is that it is not returning the expected datetime. I am getting 1/1/0001 12am:

enter image description here

What am I doing wrong?

EDIT: THis is the provider being used, per request: https://components.xamarin.com/view/sqlite-net

Edit 2: Requested SyncAudit Class:

class SyncAudit
{
    public string Server { get; set; }
    public string Database { get; set; }
    public DateTime SyncTime { get; set; }
    public int Successful { get; set; }
}
like image 692
David Tunnell Avatar asked Jul 10 '15 15:07

David Tunnell


1 Answers

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

like image 183
Sachu Avatar answered Sep 16 '22 16:09

Sachu