Projecting To EntityCollection failing

Posts   
 
    
jtoast
User
Posts: 12
Joined: 05-Nov-2007
# Posted on: 05-Nov-2007 21:47:49   

This is the first time i have tried using projectors. The following code is supposed to grab a list and project it onto an entity collection. However, all the properties in the returned collection have the value 0!

I have run the code in the debugger and confirmed that the SQL query is generating correctly, but for some reason all the values that get projected into the collection get converted to 0.

Any help appreciated.

public static EntityCollection<ProductsRelatedEntity> FetchRelatedProducts(int[] productIDs)
{
    //create collection we want the final results in
    EntityCollection<ProductsRelatedEntity> relatedProducts = new EntityCollection<ProductsRelatedEntity>();
    
    //create result set fields
    ResultsetFields fields = new ResultsetFields(2);
    fields[0] = ProductsRelatedFields.Pk_fk_RelatedProductID;
    fields[1] = ProductsRelatedFields.Pk_fk_ProductRelatedTypeID;

    //create projector
    DataProjectorToIEntityCollection2 projector = new DataProjectorToIEntityCollection2( relatedProducts );

    //create valueProjectors
    List<IDataValueProjector> valueProjectors = new List<IDataValueProjector>();
    valueProjectors.Add(new DataValueProjector(ProductsRelatedFields.Pk_fk_RelatedProductID.ToString(), 0, typeof(int)));
    valueProjectors.Add(new DataValueProjector(ProductsRelatedFields.Pk_fk_ProductRelatedTypeID.ToString(), 1, typeof(string)));
                
    //fire up adaptor
    DataAccessAdapter adapter = new DataAccessAdapter();

    //set filter
    IRelationPredicateBucket bucket = new RelationPredicateBucket();
    bucket.PredicateExpression.Add(ProductsRelatedFields.Pk_fk_ProductID == productIDs);

    //fetch
    adapter.FetchProjection(valueProjectors, projector, fields, bucket, 15, false);

    return relatedProducts;
}

Version: 2.5 Templates: Adapter

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Nov-2007 09:58:58   

Try the following instead:

List<IDataValueProjector> valueProjectors = new List<IDataValueProjector>();
    valueProjectors.Add(new DataValueProjector(ProductsRelatedFields.Pk_fk_RelatedProductID.Name, 0, typeof(int)));
    valueProjectors.Add(new DataValueProjector(ProductsRelatedFields.Pk_fk_ProductRelatedTypeID.Name, 1, typeof(string)));
jtoast
User
Posts: 12
Joined: 05-Nov-2007
# Posted on: 06-Nov-2007 15:18:46   

Thanks Walla - good eye! That was my mistake... I had meant to use the field enumeration.ToString() - I hadn't noticed i was using the actual field - doh!

jtoast
User
Posts: 12
Joined: 05-Nov-2007
# Posted on: 06-Nov-2007 22:05:31   

Hey Walaa

One more question to this end:

My entity collection actually has three columns - though it is only two that are being projected into. Is there a way to set a default value into the third column? Perhaps something along the lines of:

valueProjectors.Add(new DataValueProjector("Default Value", 2, typeof(string)));

I can easily loop through and set the value afterwards - i just just curious if there was a way to do it as part of the projection code.

Thanks for any input

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 07-Nov-2007 10:04:07   

valueProjectors.Add(new DataValueProjector("Default Value", 2, typeof(string)));

Well the first parameter is the field name, not its value, and the second parameter is the index of the field in the dynamicList from where the value will be copied.

I can see no way of doing this, unless the DynamicList already had a column holding this default value.

So it's better to update the entityCollection to set the default value of the field of the entities withtin.

You may also have this default value, initialized from within the entity, using any of the entity's initialization routines (InitMembers...or InitClassEmpty).