简体   繁体   中英

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:

在此输入图像描述

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:

在此输入图像描述

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; }
}

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.

I often have this issue using Select with SQLite . I don't know where the problem actually is, but it seems like lazy loading is broken so you end up getting default values when it comes time to evaluate the query. I solve this by forcing evaluation before the Select . It's annoying but it works:

Broken example (my own):

        using (var db = new SQLiteConnectionWithLock(base.DatabasePath))
        {
            return db.Table<VideoUploadChunk>()
                .Where(c => c.VideoId == videoId)
                .Select(c => c.ChunkNumber)
                .ToList();
        }

This should return a list of ints , but it ends up returning a bunch of 0's (the default).

Fixed example:

        using (var db = new SQLiteConnectionWithLock(base.DatabasePath))
        {
            var result = db.Table<VideoUploadChunk>()
                .Where(c => c.VideoId == videoId)
                .ToList();

            return result.Select(c => c.ChunkNumber);
        }

Your example (hopefully fixed):

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

        return query.Select(c => c.SyncTime).ToString();
    }

SQLite don't have special type for date and time , instead it use either number or text to store such data. SQLiteConnection constructor has a parameter storeDateTimeAsTicks which tell your provider how it should interpet and store date and time values. It is true by default.

So maybe SyncTime is stored in your database as a text rather than a number. Try to adjust connection so provider can interpret data correctly by passing false as storeDateTimeAsTicks :

var db = new SQLite.SQLiteConnection(this.DBPath, storeDateTimeAsTicks: false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM