Hi guys,
I recently needed the ability to convert a retrieval procedure into an entity collection and after spending some time with the new FetchProjection methods, managed to come up with the following code.
The retrieval query should return all of the fields the entity uses.
public class MyDataAdapter : DataAccessAdapter
{
public void FetchEntityCollectionUsingRetrievalQuery( IEntityCollection2 collectionToFill, IPrefetchPath2 prefetchPath, IRetrievalQuery queryToExecute )
{
IEntityFactory2 entityFactory = collectionToFill.EntityFactoryToUse;
if ( entityFactory == null )
{
throw new ArgumentException( "No entity factory specified in the passed in IEntityCollection2 object. Cannot continue", "collectionToFill" );
}
IEntityFields2 entityFields = entityFactory.CreateFields();
List<IDataValueProjector> valueProjectors = new List<IDataValueProjector>( entityFields.Count );
foreach ( EntityField2 entityField in entityFields )
{
valueProjectors.Add( new DataValueProjector( entityField.Alias, entityField.FieldIndex, entityField.DataType ) );
}
FetchProjection( valueProjectors, new DataProjectorToIEntityCollection2( collectionToFill ), queryToExecute );
FetchPrefetchPath( collectionToFill, null, 0, null, prefetchPath );
}
}
This method will need to appear in the DataAccessAdapter class because it uses the protected FetchPrefetchPath method to support prefetching of additional entities.
HTH,
Shannon