Hi James,
The link I posted above explain that. Here is a little example. 1st method get the DataReader, second method project it (only two fields in this case) to a EntityCollection:
static IDataReader GetCustomerDataReader()
{
SqlConnection conn = null;
string connString = "data source=ACATENANGO;initial catalog=Northwind;integrated security=SSPI;persist security info=False;packet size=4096";
conn = new SqlConnection(connString);
string query = "SELECT * FROM Customers";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
static void ProjectDataReaderIntoCollection()
{
EntityCollection customers = new EntityCollection(new CustomersEntityFactory());
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
using (IDataReader reader = GetCustomerDataReader())
{
// first resultset: Customers.
List<IDataValueProjector> valueProjectors = new List<IDataValueProjector>();
// project value on index 0 in resultset row onto CustomerId
valueProjectors.Add(new DataValueProjector(CustomersFieldIndex.CustomerId.ToString(), 0, typeof(string)));
// project value on index 1 in resultset row onto CompanyName
valueProjectors.Add(new DataValueProjector(CustomersFieldIndex.CompanyName.ToString(), 1, typeof(string)));
// resultset contains more rows, we just project those 2. The rest is trivial.
DataProjectorToIEntityCollection2 projector = new DataProjectorToIEntityCollection2(customers);
adapter.FetchProjection(valueProjectors, projector, reader);
reader.Close();
}
}