Ninja Database Pro
AddIndex<T,TIndex>(String,String,IndexStyle) Method
Example 



KellermanSoftware.NinjaDatabasePro Namespace > NinjaDbPro Class > AddIndex Method : AddIndex<T,TIndex>(String,String,IndexStyle) Method
The class type
A unique name for the index for this type
The name of the property to index
The style of the index, unique or non unique
Add an index for a single property of a type.
Syntax
Public Overloads Sub AddIndex
    (Of T As {Class, New},
     TIndex)( _
   ByVal indexName As String, _
   ByVal propertyName As String, _
   ByVal indexStyle As IndexStyle _
) 
Dim instance As NinjaDbPro
Dim indexName As String
Dim propertyName As String
Dim indexStyle As IndexStyle
 
instance.AddIndex(Of T, TIndex)(indexName, propertyName, indexStyle)
public void AddIndex<T,TIndex>( 
   string indexName,
   string propertyName,
   IndexStyle indexStyle
)
where T: class, new()
public:
void AddIndexgeneric<typename T>
generic<typename TIndex>
( 
   String^ indexName,
   String^ propertyName,
   IndexStyle indexStyle
) 
where T: ref class, gcnew()

Parameters

indexName
A unique name for the index for this type
propertyName
The name of the property to index
indexStyle
The style of the index, unique or non unique

Type Parameters

T
The class type
TIndex
Example
NinjaDbPro db = new NinjaDbPro("SingleIndexExampleDir", "SingleIndexExampleDir.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>("NameIndex"))
    db.AddIndex<Person,string>("NameIndex", "Name", IndexStyle.NonUnique);
 
//Add a record to the database
Person person = new Person();
person.Name = "John Smith";
person.DateCreated = DateTime.Now;
db.Save(person);
 
//Find an exact matach.  If the index is unique, this is the fastest.
var exactQuery = db.CreateIndexQuery<Person, string>("NameIndex", "John Smith");
 
//Lazy load the first record and write out a property
Console.WriteLine(exactQuery[0].LazyValue.DateCreated);
 
//Find a partial match
var partialQuery = db.CreateIndexQuery<Person, string>("NameIndex").Where(o => o.Index.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("SingleIndexExampleDir", "SingleIndexExampleDir.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)("NameIndex") Then
    db.AddIndex(Of Person,String)("NameIndex", "Name", 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)
 
'Find an exact matach.  If the index is unique, this is the fastest.
Dim exactQuery = db.CreateIndexQuery(Of Person, String)("NameIndex", "John Smith")
 
'Lazy load the first record and write out a property
Console.WriteLine(exactQuery(0).LazyValue.DateCreated)
 
'Find a partial match
Dim partialQuery = db.CreateIndexQuery(Of Person, String)("NameIndex").Where(Function(o) o.Index.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