Litedb: QUERY EMBEDDED DOCUMENTS

Created on 24 May 2019  路  1Comment  路  Source: mbdavid/LiteDB

I have this POCO classes

class Author 
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool Sex { get; set; }
    }
 class Book
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public List<Author> Authors { get; set; }
    }

How can I query the "Authors" prop to find, for example, all books written by an author?
I have tried many syntax but anythings work.
@mbdavid (I think my problem is how to inspect all elements in the list in order to retrieve the matched condition)

Most helpful comment

var Books = DB.GetCollection<Book>("Books");
var Query= Books.Find(Query.And(Query.Not("Authors",BsonValue.Null),Query.EQ("Authors[*].Name","Shakespeare")));

I haven't tested it by it should work.

>All comments

var Books = DB.GetCollection<Book>("Books");
var Query= Books.Find(Query.And(Query.Not("Authors",BsonValue.Null),Query.EQ("Authors[*].Name","Shakespeare")));

I haven't tested it by it should work.

Was this page helpful?
0 / 5 - 0 ratings