Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4077428
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:32:56+00:00 2026-05-20T17:32:56+00:00

Suppose this is what I have in my database table Ancestor ( idAncestor int

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:32:57+00:00Added an answer on May 20, 2026 at 5:32 pm

    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;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

suppose that I have this RDBM table ( Entity-attribute-value_model ): col1: entityID col2: attributeName
Suppose I have a database table with two fields, foo and bar. Neither of
Suppose I have a database table with columns a, b, and c. I plan
Suppose that I have a database table that contains information on cities across the
I have a database table, with people identified by a name, a job and
Suppose I have two queries on a database table. The queries are defined in
Suppose I have a MySQL database named DB, and a table named ContactInfo. The
I have 10,000 rows in database table. I have a view which is suppose
I am using NHibernate and this is not a Homework. Suppose I have retrieved
I've seen second one in another's code and I suppose this length comparison have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.