Copying from EntityCollection to EntityCollection impossible?

后端 未结 3 848
我在风中等你
我在风中等你 2021-01-22 22:13

How would you do this (pseudo code): product1.Orders.AddRange(product2.Orders);

However, the function \"AddRange\" does not exist, so how would you copy all items in the

3条回答
  •  被撕碎了的回忆
    2021-01-22 22:42

    Based on the previous two answers, I came up with the following working solution:

    public static void AddRange(this EntityCollection destinationEntityCollection,
                                           EntityCollection sourceEntityCollection) where T : class  
        {
            var array = new T[sourceEntityCollection.Count()];
    
            sourceEntityCollection.CopyTo(array,0);
    
    
            foreach (var entity in array)
            {
                destinationEntityCollection.Add(entity);
            }
        }
    

提交回复
热议问题