NinjaDbPro db = new NinjaDbPro("CompositeIndexExampleDir", "CompositeIndexExampleDir.db");
//Licensed Mode
//db.UserName = "John Smith 101224";
//db.LicenseKey = "aousdf832jasf==";
//Set before OpenDatabase. Default storage is IsolatedStorageDatabase. Other options are:
//db.Storage = new MemoryDatabase(); //In memory database
//db.Storage = new FileDatabase();
//Open the database
db.OpenDatabase();
//Add a single property index if it doesn't already exist
if (!db.IndexExists<Person>("NameCreatedIndex"))
{
List<string> indexProperties = new List<string>();
indexProperties.Add("Name");
indexProperties.Add("DateCreated");
db.AddCompositeIndex<Person>("NameCreatedIndex", indexProperties,IndexStyle.NonUnique);
}
//Add a record to the database
Person person = new Person();
person.Name = "John Smith";
person.DateCreated = DateTime.Now;
db.Save(person);
//Build the index to find. This has to be in the same order as the index
List<object> indexValues = new List<object>();
indexValues.Add(person.Name);
indexValues.Add(person.DateCreated);
//Find an exact matach. If the index is unique, this is the fastest.
var exactQuery = db.CreateCompositeIndexQuery<Person>("NameCreatedIndex", indexValues);
//Lazy load the first record and write out a property
Console.WriteLine(exactQuery[0].LazyValue.DateCreated);
//Find a partial match
var partialQuery = db.CreateCompositeIndexQuery<Person>("NameCreatedIndex").Where(o => o.Indexes[0].ToString().StartsWith("John"));
//Write the DateCreated for all records that have a name beginning with John
foreach (var indexRecord in partialQuery)
{
Console.WriteLine(indexRecord.LazyValue.DateCreated);
}
//Close the database
db.CloseDatabase();