I'm looking for best practices here. Sorry. I know it's subjective, but there are a lot of smart people here, so there ought to be some "very good" ways of doing this.
I have a custom object called Employee. That object has seven properties like name, phone, email, and so on. There is also a table in my SQL database called tblEmployees with seven columns labeled somewhat similarly. My goal is to "convert" the results from a query to a generic list of Employee objects. What is the best way to do this (lowest overhead, quickest)?
What I am doing currently is something that I've seen proposed all over the web. I don't like it because I feel like it slows down my page loads. Generic lists make me faster at what I do, but I don't feel good about making my customers pay the price.
Here's what I'm doing:
List<Employee> list = new List<Employee>();
DataSet ds = Employee.searchEmployees("Byron");
foreach (DataRow dr in ds.Tables[0].Rows)
{
list.Add(new Employee(dr));
}
I have a constructor that takes a DataRow (as shown) which handles the 'property = dr["column"]' stuff.
Looking forward to your thoughts.
Briefly looking at the code, and not seeing how it is used, I would return a IEnumerator instead of a list. You can then use the yield return statement and you won't be looping through the list twice (one to populate and one to display).
so...
protected IEnumerable<Employee> GetEmployees ()
{
List<Employee> list = new List<Employee>();
DataSet ds = Employee.searchEmployees("Byron");
foreach (DataRow dr in ds.Tables[0].Rows)
{
yield return new Employee(dr);
}
}