Generic Collections Issue.

Posts   
 
    
smcleod_au
User
Posts: 13
Joined: 27-Jul-2005
# Posted on: 03-Sep-2006 23:40:35   

Hi, I'm feeling a little stupid right now, but was hoping for some more guidance on generics and the use of IEntityCollection2 and IEntity2. I'm using the self servicing option (is this my issue?) and want to do some pretty straight forward stuff with Generic Collections.

I don't think the scenario matters too much, but I'll use it for the sake of explanation. I want to build a TreeView (in Windows Forms) that takes a collection and then builds all the corresponding entities under that node.

For instance my class looks like this.


    public class CollectionTreeNode : TreeNode
    {
        public CollectionTreeNode()
        {
        }

        public TreeNode ConstructCollectionCollection()
        {
            TreeNode parentNode = new TreeNode(_collection.GetType().FullName);

            if (this._collection == null)
                throw new Exception("Collection must not be null");
            else
            {
                foreach (IEntity2 entity in _collection)
                {
                    parentNode.Nodes.Add(entity.Fields["Full Name"].CurrentValue.ToString());
                }
            }

            return parentNode;
        }

        private IEntityCollection2 _collection;
        public IEntityCollection2 Collection
        {
            get { return _collection; }
            set { _collection = value; }
        }
   }

And from my code I create a collection of a known type, and fill that with data, create an instance of my CollectionTreeNode class and add the collection to it.


            treeView2.BeginUpdate();
            CollectionTreeNode ctn = new CollectionTreeNode();
            CustomerCollection cc = new CustomerCollection();
            cc.GetMulti(null);
            ctn.Collection = (IEntityCollection2)cc;
            treeView2.Nodes.Add(ctn.ConstructCollectionCollection());
            treeView2.EndUpdate();

However, when I cast my collection in to IEntityCollection2 I get an error: {"Unable to cast object of type 'DataAccessLayer.CollectionClasses.CustomerCollection' to type 'SD.LLBLGen.Pro.ORMSupportClasses.IEntityCollection2'."}

What am I missing here? I'm sure I've missed some of the theory about generics, but I'm having a hard time getting this going... Any help would be appreciated.

Cheers,

Stuart.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Sep-2006 07:10:52   

The "2" classes are for the Adapter model, not for the SelfServicing one.

So use IEntityCollection and IEntity instead.

smcleod_au
User
Posts: 13
Joined: 27-Jul-2005
# Posted on: 04-Sep-2006 08:13:39   

Works great. Thanks.

smcleod_au
User
Posts: 13
Joined: 27-Jul-2005
# Posted on: 04-Sep-2006 10:03:42   

Is there any way to access custom fields of an entity via IEntity?

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 04-Sep-2006 15:30:08   

Do you mean the generated properties? There is no direct way to do that, except for Reflection, which you can use in sync with the consistent naming.

Now if you want the fields value, just go the way you did in your code, and for relation based preoperties, you may try GetDependent/GetDependingEntities.

Exploring the Relations() property with reflection is also a good start to hack into the generated code.