Projecting onto an Entity Collection

Posts   
 
    
Posts: 15
Joined: 13-Mar-2011
# Posted on: 17-Mar-2011 04:03:47   

Hi,

I have tried to follow the example in the documentation for projecting an IDataReader result onto an Entity Collection (which I'll reproduce here for convenience):


// C#
EntityCollection<CustomerEntity> customers = new EntityCollection<CustomerEntity>(new CustomerEntityFactory() );
EntityCollection<OrderEntity> orders = new EntityCollection<OrderEntity>( new OrderEntityFactory() );
using(IRetrievalQuery query = RetrievalProcedures.GetCustomersAndOrdersOnCountryCallAsQuery( "Germany" ))
{
    using(DataAccessAdapter adapter = new DataAccessAdapter())
    {
        using(IDataReader reader = adapter.FetchDataReader(query, CommandBehavior.CloseConnection))
        {
            // first resultset: Customers.
            List<IDataValueProjector> valueProjectors = new List<IDataValueProjector>();
            // project value on index 0 in resultset row onto CustomerId
            valueProjectors.Add( new DataValueProjector( CustomerFieldIndex.CustomerId.ToString(), 0, typeof( string ) ) );
            // project value on index 1 in resultset row onto CompanyName
            valueProjectors.Add( new DataValueProjector( CustomerFieldIndex.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 );
            // second resultset: Orders.
            valueProjectors = new ArrayList();
            valueProjectors.Add( new DataValueProjector( OrderFieldIndex.OrderId.ToString(), 0, typeof( int ) ) );
            valueProjectors.Add( new DataValueProjector( OrderFieldIndex.CustomerId.ToString(), 1, typeof( string ) ) );
            valueProjectors.Add( new DataValueProjector( OrderFieldIndex.OrderDate.ToString(), 3, typeof( DateTime ) ) );
            // switch to the next resultset in the datareader
            reader.NextResult();
            projector = new DataProjectorToIEntityCollection2( orders );
            adapter.FetchProjection( valueProjectors, projector, reader );
            reader.Close();
        }
    }
}

Here is the code which I have written to implement that (database is AdventureWorks):


        public EntityCollection<ProductModelEntity> GetProductModels()
        {
            EntityCollection<ProductModelEntity> productModels = new EntityCollection<ProductModelEntity>();

            ResultsetFields fields = new ResultsetFields(6);
            fields.DefineField(ProductModelFields.ProductModelId, 0, "ProductModelID");
            fields.DefineField(ProductModelFields.Name, 1, "Name");
            fields.DefineField(ProductModelFields.CatalogDescription, 2, "CatalogDescription");
            fields.DefineField(ProductModelFields.Instructions, 3, "Instructions");
            fields.DefineField(ProductModelFields.Rowguid, 4, "rowguid");
            fields.DefineField(ProductModelFields.ModifiedDate, 5, "ModifiedDate");

            using (DataAccessAdapter adapter = DataAccess.AdaptorFactory.GetNewAdaptor(true))
            {
                using (IDataReader reader = adapter.FetchDataReader(fields, null, CommandBehavior.SingleResult, 50, true))
                {
                    List<IDataValueProjector> valueProjectors = new List<IDataValueProjector>();
                    valueProjectors.Add(new DataValueProjector(ProductModelFields.ProductModelId.ToString(), 0, typeof(int)));
                    valueProjectors.Add(new DataValueProjector(ProductModelFields.Name.ToString(), 0, typeof(string)));
                    DataProjectorToIEntityCollection2 projector = new DataProjectorToIEntityCollection2(productModels);
                    adapter.FetchProjection(valueProjectors, projector, reader);
                }
            }

            return productModels;
        }

The first thing I don't get is where the CustomerFieldIndex object comes from in the documentation sample. There is no equivalent in the code which I generated. So I tried a bunch of things to substitute for it. The code I showed above just returns 50 tuples with 0 in the ProductModelID field and either an empty string, or null, for the Name field.

Can anyone identify what I have done wrong? Thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 17-Mar-2011 10:05:44   

CustomerFieldIndex should be available just under your root Namepsace.

(Edit)

valueProjectors.Add(new DataValueProjector(ProductModelFields.ProductModelId.ToString(), 0, typeof(int))); valueProjectors.Add(new DataValueProjector(ProductModelFields.Name.ToString(), 0, typeof(string)));

Wtch out for the index parameter, you have passed 0 for both fields.

Posts: 15
Joined: 13-Mar-2011
# Posted on: 18-Mar-2011 02:17:50   

Thanks again Walaa.

I actualy did a comprehensive "Find" of my solution yesterday looking for the ProductModelFieldIndex enum, and it yielded no results. Hence my post.

But I just peeked inside the DAL ConstantsEnums.cs file and it was right there. That's really annoying confused

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Mar-2011 05:28:38   

onefootswill wrote:

Thanks again Walaa.

I actualy did a comprehensive "Find" of my solution yesterday looking for the ProductModelFieldIndex enum, and it yielded no results. Hence my post.

But I just peeked inside the DAL ConstantsEnums.cs file and it was right there. That's really annoying confused

Hi there. So.. Should we assume your question is answered/solved?

David Elizondo | LLBLGen Support Team