Creating Extension Method to map entity with subentities object to Poco object I am trying to create an extension method that builds a POCO object (copies all the fields) for an Entity Object.

When Entity object is simple (no navigation, no sub collections), it works fine. 
I want to improve it so it could also deal with sub-entities. 

For this example I take Nortwind database and use Customer and Order entities. 
My POCOs:

 public class CustomerPOCO : BaseDataItemModel
    {
        public string CustomerID { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }

        List&lt;OrderPOCO&gt; orders;

       [PropertySubCollection]
        public List&lt;OrderPOCO&gt; Orders
        {
            get { return orders; }
            set { orders = value; }
        }


        public CustomerPOCO()
        {
            orders = new List&lt;OrderPOCO&gt;();
        }

    }


public class OrderPOCO : BaseDataItemModel
{

   public int OrderID { get; set; }
   public string ShipName { get; set; }
   public string ShipCity { get; set; }

}


Extension Method:

 public static void CopyPropertiesFrom&lt;TObjectTarget&gt;(
           this ICollection&lt;TObjectTarget&gt; targetitems, System.Collections.IEnumerable sourceitems)
            where TObjectTarget : new()
        {
            foreach (var source in sourceitems)
            {

                TObjectTarget targetObject = new TObjectTarget();

                PropertyInfo[] allProporties = source.GetType().GetProperties();

                PropertyInfo targetProperty;
                foreach (PropertyInfo fromProp in allProporties)
                {
                    targetProperty = targetObject.GetType().GetProperty(fromProp.Name);



                    if (targetProperty == null) continue;
                    if (!targetProperty.CanWrite) continue;

                    //check if property in target class marked with SkipProperty Attribute
                    if (targetProperty.GetCustomAttributes(typeof(SkipPropertyAttribute), true).Length != 0) continue;


                    //Sub Collection -&gt; set it here
                    if (targetProperty.GetCustomAttributes(typeof(PropertySubCollection), true).Length != 0)
                    {

                    }
                    else

                        targetProperty.SetValue(targetObject, fromProp.GetValue(source, null), null);


                }

                targetitems.Add(targetObject);

            }
        }

static void Main(string[] args)
 {
      List&lt;CustomerPOCO&gt; customers;
      using (NorthwindEntities northcontext = new NorthwindEntities())
      {
          var cus = from customer in northcontext.Customers
                  select customer;

          customers = new List&lt;CustomerPOCO&gt;();
          customers.CopyPropertiesFrom(cus);              

      }
   }


Note: Extension should know the type of sub-entity and a type of sub-poco...
For this task, i would use AutoMapper, it does exactly what you need, and can work with complex entities.

If you are writing this mapper yourself, consider using expression compilation feature.
Such code will execute at the speed of a custom written code, with all reflection interpretation done beforehand. See the code example for Expression.MemberInit

As for the sub-objects in your code, you will figure out the recursive procedure, as soon as you rewrite your extension to work with a Type argument, not a generic argument. This way you can easily call the procedure for the properties, knowing their type from PropertyInfo.