Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

479
Visualizações
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; }
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda