Litedb: How to filter using SQL syntax

Created on 1 Sep 2019  路  1Comment  路  Source: mbdavid/LiteDB

Hi there, I have a quick question.

I'm trying out the new v5-aplha version. From what I understand this should support a new SQL-like query syntax? But the documentation it still a bit scarce about this.

Is it possible to use a SQL-like query from code? For exmaple, something like:

using (var db = new LiteDatabase("test.db"))
{
    var result = db.Query("SELECT $ FROM Books WHERE Title = 'Acme'");
}

Thanks in advance.

question

Most helpful comment

Hi @chinookproject,

I'm glad to inform you that you were really close to it.
The code you need to is something like this (which gives you a list of BsonDocuments).

using (var db = new LiteDatabase(new MemoryStream()))
{
    using (var bsonReader = db.Execute("SELECT $ FROM Books WHERE  Title = 'Acme'"))
    {
        var output = new List<BsonValue>();

        while (bsonReader.Read())
        {
            output.Add(bsonReader.Current);
        }

        return output;
    }
}

Please note though that you will still need to do the conversion from BsonDocuments to actual class objects yourself (unless there's some way I don't know about yet).

I think this answers your question so I'm going to close the issue. If it hasn't, feel free to re-open the issue and tell what's still missing. (Please also note that v5 is still in the beta stage and documentation isn't finished yet) 馃檪

>All comments

Hi @chinookproject,

I'm glad to inform you that you were really close to it.
The code you need to is something like this (which gives you a list of BsonDocuments).

using (var db = new LiteDatabase(new MemoryStream()))
{
    using (var bsonReader = db.Execute("SELECT $ FROM Books WHERE  Title = 'Acme'"))
    {
        var output = new List<BsonValue>();

        while (bsonReader.Read())
        {
            output.Add(bsonReader.Current);
        }

        return output;
    }
}

Please note though that you will still need to do the conversion from BsonDocuments to actual class objects yourself (unless there's some way I don't know about yet).

I think this answers your question so I'm going to close the issue. If it hasn't, feel free to re-open the issue and tell what's still missing. (Please also note that v5 is still in the beta stage and documentation isn't finished yet) 馃檪

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lidanger picture lidanger  路  3Comments

nightroman picture nightroman  路  3Comments

kuiperzone picture kuiperzone  路  4Comments

manny42 picture manny42  路  3Comments

furesoft picture furesoft  路  4Comments