Skip to content

Instantly share code, notes, and snippets.

@aleksandrzak-rafal
Created March 30, 2016 11:24
Show Gist options
  • Select an option

  • Save aleksandrzak-rafal/140110b4ca455695cb7249c1e9959660 to your computer and use it in GitHub Desktop.

Select an option

Save aleksandrzak-rafal/140110b4ca455695cb7249c1e9959660 to your computer and use it in GitHub Desktop.
Clever way to map data from DataReader to class
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public static Employee Create(IDataRecord record)
{
return new Employee
{
Id = record["id"],
Name = record["name"]
};
}
}
public IEnumerable<Employee> GetEmployees()
{
using (var reader = YourLibraryFunction())
{
while (reader.Read())
{
yield return Employee.Create(reader);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment