score:1

Accepted answer

I am not sure why you're doing that. Can I suggest this?

You do not need to go all that way of creating a class that joins the two. Just create a Navigation property on your RP that points to RPHistory Objects.

public class RP 
{
    public int ID { get; set; }
    public int RPID { get; set; }
    public string Name { get; set; }
    public int ProductID { get; set; }
    public ICollection<RPHistory> HistoryList { get; set; } // Navigation Property
}

public class RPHistory: 
{
    public int ID { get; set; }
    public int RPID { get; set; }
    public string Name { get; set; }
    public int ProductID { get; set; }

    [ForeignKey(nameof(RPID))] // Identify the Foreign Key from RP Class
    public RP RP { get; set; } // Navigation back to RP
}

Then you can chain everything into a single list using LINQ:

var RPs = Context.RP.Where(rp => rp.ProductID  == request.ID)
                 .Include(rp=>rp.RPHistory) // This includes RPHistory
                 .ToList();

score:1

You need to clone or create a new list.

Option 1: Use ConvertAll

List<RPHistory> pPHistoryCopy = rphWithHistory.RPHistory.ConvertAll(history => new RPHistory(rphWithHistory.RPHistory));

Option 2:

//Clone Extension
static class Extensions
{
    public static IList<T> Clone<T>(this IList<T> listToClone) where T: ICloneable
    {
        return listToClone.Select(item => (T)item.Clone()).ToList();
    }
}

Use the clone extention


Related Articles