2

Suppose this is what I have in my database


table Ancestor (  
  idAncestor int not null,  
  name varchar(20) not null,  
)  

table Descendant (  
  idDescendant int not null,  
  name varchar(20) not null,  
  Ancestor_idAncestor int not null  
)  

When ADO.NET generates the entity object for the above 2 tables, I can access Descendant of Ancestor through Ancestors.First().Descendants.

If I were to recursively iterate over an ancestor's descendant(s) or descendant's descendant(s) and print out its id and name, the following is my attempt


public void Iterate(Ancestor a)  
{  
   Type t = a.GetType();
   PropertyInfo[] props = t.GetProperties();
   foreach(var prop in props){
      // pseudo code here
      if(prop is entitycollection)
      {
         // how do I convert prop to entity collection here??
         foreach(var p in prop){
            Iterate(p)
         }         
      } else {
         print prop.GetValue(a, null)
      }
   }       
}

My problem is trying to figure out if a entity property is of entity collection, if it is, then find the type hold by the collection, then iterate over the type, and so on.

Thanks

nobody
  • 2,709
  • 6
  • 35
  • 37

1 Answers1

5

This should be a crude version of what you're looking for:

private static void recurseAndPrintProperties(Object ObjectToRecurse) {
   foreach (PropertyInfo pi in ObjectToRecurse.GetType().GetProperties()) {
       if ((pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(EntityCollection<>))) {
           IEnumerable collection = (IEnumerable)pi.GetValue(ObjectToRecurse, null);

           foreach (object val in collection)
               recurseAndPrintProperties(val);
       } else {
            if (pi.PropertyType == typeof(Descendant)) {
                Descendant actualDescendant = (Descendant)pi.GetValue(ObjectToRecurse, null);
                Console.WriteLine(actualDescendant.idDescendant + " - " + actualDescendant.Name);
            } else
                Console.WriteLine(pi.Name + "  -  " + pi.GetValue(ObjectToRecurse, null));
       }
   }
}

EDIT

This code came from some code I had previously played around with for cloning entities. Here it is (you might have to modify it to also take into consideration not duplicating your properties thar are mapped to primary keys)

    public static T CloneEntity<T>(T Obj) where T : EntityObject, new() {
        T Clone = new T();

        Type typeToClone = Obj.GetType();
        Type[] BadGenericTypes = new Type[] { typeof(EntityCollection<>), typeof(EntityReference<>) };
        Type[] BadTypes = new Type[] { typeof(System.Data.EntityKey) };

        foreach (PropertyInfo pi in typeToClone.GetProperties().Where(p => p.CanWrite)) {
            if (pi.PropertyType.IsGenericType && BadGenericTypes.Contains(pi.PropertyType.GetGenericTypeDefinition())
                || (BadTypes.Contains(pi.PropertyType))
                || (pi.Name.Equals(Extension.GetPropertyName(() => new FMVHistory().FMVHistoryId), StringComparison.CurrentCultureIgnoreCase)))
                continue;

            pi.SetValue(Clone, pi.GetValue(Obj, null), null);
        }
        return Clone;
    }
Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
  • Thx for the input, I was able to move forward a bit because of it. However, my problem is not trying to ignore the EntityCollection type. I am trying to detect if an property is an EntittyCollection type, then iterate on that type. – nobody Mar 21 '11 at 19:24
  • Thanks you so much, this is the line of the code that helped me `IEnumerable collection = (IEnumerable)pi.GetValue(ObjectToRecurse, null);` – nobody Mar 21 '11 at 19:54
  • Also, the code snippet for cloning entity was helpful for my other purpose. Wish you could have leave it as part of the answer for other to see. – nobody Mar 21 '11 at 19:56
  • You can see it in the revisions - I'll re-add it – Adam Rackis Mar 21 '11 at 20:49