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

558
Visualizações
C# MongoDb Driver Convert string to DateTime and for Filter Builder
 var builder = Builders<ModelClass>.Filter;
 var filter = builder.Where(x => x.Active);

if (fromDate.HasValue)
        {
            var date = fromDate.Value;
            var subfilter = builder.Where(x =>DateTime.Parse(x.EnrollmentDate) >= date);
            filter &= subfilter;
        }

Enrolment Date is Saved as a string public string EnrollmentDate{ get; set; }, I need to Filter Docs within a Set Date Range, But How do i Compare this? i need to filter like this,

I get System.InvalidOperationException: Parse({document}{EnrollmentDate}) is not supported. Error in SubFilter Line

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You have problem here when you want to do DateTime.Parse()

Can you post format of your string EnrollmentDate? And your variable date , is it only Date or DateTime?

This one maybe can help you here

Also, try to use

var subfilter = builder.Gte(x=>x.Y, Z) 
over 4 years ago · Santiago Trujillo Relatório

0

I think you need to achieve with MongoDB query as below:

{
  "$expr": {
    "$gte": [
      { "$toDate": "$EnrollmentDate" },
      date
    ]
  }
}

While I think it is not achievable with MongoDB .Net Driver LINQ syntax, you convert the query as BsonDocument:

var subfilter = new BsonDocument("$expr",
    new BsonDocument("$gte", 
        new BsonArray {
            new BsonDocument("$toDate", "$EnrollmentDate"),
            date
        }
    )
);

filter &= subfilter;
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