Ninja Database Pro
CreateCompositeIndexQuery<T>(String) Method
Example 



KellermanSoftware.NinjaDatabasePro Namespace > NinjaDbPro Class > CreateCompositeIndexQuery Method : CreateCompositeIndexQuery<T>(String) Method
Query all index records for the specified composite property index
Syntax
Public Overloads Function CreateCompositeIndexQuery(Of T As {Class, New})( _
   ByVal indexName As String _
) As List(Of CompositeIndexRecord(Of T))
Dim instance As NinjaDbPro
Dim indexName As String
Dim value As List(Of CompositeIndexRecord(Of T))
 
value = instance.CreateCompositeIndexQuery(Of T)(indexName)
public List<CompositeIndexRecord<T>> CreateCompositeIndexQuery<T>( 
   string indexName
)
where T: class, new()
public:
List<CompositeIndexRecord<T^>^>^ CreateCompositeIndexQuerygeneric<typename T>
( 
   String^ indexName
) 
where T: ref class, gcnew()

Parameters

indexName

Type Parameters

T
Example
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();
Dim db As 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 Not db.IndexExists(Of Person)("NameCreatedIndex") Then
    Dim indexProperties As New List(Of String)()
    indexProperties.Add("Name")
    indexProperties.Add("DateCreated")
    db.AddCompositeIndex(Of Person)("NameCreatedIndex", indexProperties,IndexStyle.NonUnique)
End If
 
'Add a record to the database
Dim person As New Person()
person.Name = "John Smith"
person.DateCreated = Date.Now
db.Save(person)
 
'Build the index to find.  This has to be in the same order as the index
Dim indexValues As New List(Of Object)()
indexValues.Add(person.Name)
indexValues.Add(person.DateCreated)
 
'Find an exact matach.  If the index is unique, this is the fastest.
Dim exactQuery = db.CreateCompositeIndexQuery(Of Person)("NameCreatedIndex", indexValues)
 
'Lazy load the first record and write out a property
Console.WriteLine(exactQuery(0).LazyValue.DateCreated)
 
'Find a partial match
Dim partialQuery = db.CreateCompositeIndexQuery(Of Person)("NameCreatedIndex").Where(Function(o) o.Indexes(0).ToString().StartsWith("John"))
 
'Write the DateCreated for all records that have a name beginning with John
For Each indexRecord In partialQuery
    Console.WriteLine(indexRecord.LazyValue.DateCreated)
Next indexRecord
 
'Close the database
db.CloseDatabase()
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

NinjaDbPro Class
NinjaDbPro Members
Overload List