0

I have a result object in litedb with a collection property, as below:

public class Result
{
    public int Id { get; set; }
    public ICollection<Entity> ExtractedEntities { get; set; }
}

public class Entity
{
    public string Value { get; set; }
    public int Id { get; set; }
}

Is it possible to index the properties of the entity class so that I can do something like:

collection.Find(r => r.ExtractedEntities.Any(ee => ee.Value == "test" && ee.Id == 1));

Thanks

jondow
  • 624
  • 5
  • 15

1 Answers1

0

Yes.

using(var db = new LiteDatabase(@"C:\Temp\MyData.db"))
{
    var col = db.GetCollection<Customer>("customers");

    // Index document using document Name property
    col.EnsureIndex(x => x.Name);

    // Use LINQ to query documents
    var results = col.Find(x => x.Name.StartsWith("Jo"));

    // Let's create an index in phone numbers (using expression). It's a multikey index
    col.EnsureIndex(x => x.Phones, "$.Phones[*]"); 

    // and now we can query phones
    var r = col.FindOne(x => x.Phones.Contains("8888-5555"));
}

You find this in the documentation here or here

Andrew Diamond
  • 6,295
  • 1
  • 15
  • 33