Knight Data Access Layer
Loading By Query
Basic Tasks C# > Loading Data > Loading By Query

It is possible to create a dynamic query with parameters and then perform a load with all properties automatically mapped.  Below is an example:

 

//Get the current instance of the helper

IDataHelper db = DataHelper.SessionFactory();

 

//Create a new Person to save to the database

Person currentPerson = new Person();

currentPerson.FirstName = "John";

currentPerson.LastName = "Smith";

 

//Save it to the database

db.Save(currentPerson);

 

string sql = "select * from Person where FirstName=@FirstName and LastName=@LastName";

 

//Creating parameters

Dictionary<string, object> parms = new Dictionary<string, object>();

parms.Add("FirstName", "John");

parms.Add("LastName", "Smith");

 

//Load the passed sql into a list of objects

List<Person> personList = db.Load<Person>(sql, parms);