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
Source: stackoverflow.com
Related Articles
- Cannot implicitly convert type system linq IQueryable to system collections generic List
- Cannot implicitly convert linq result into viewmodel list
- Cannot convert List to IEnumerable when combining 2 generic lists MVC C#
- LINQ - Cannot implicitly convert type with custom data model list property
- Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists
- Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.List<string>
- Cannot implicitly convert type 'System.Linq.IQueryable<int>' to 'int?'
- Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.IList'
- Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Entity.DbSet'
- Convert DataTable to Generic List in C#
- Cannot implicitly convert type 'bool' to 'system.threading.tasks.task bool'
- Cannot implicitly convert type IEnumerable<T> to IQueryable<T>
- Cannot implicitly convert type 'int?' to 'int'
- Cannot implicitly convert type '.List<AnonymousType#1>' to '.List<WebApplication2.Customer>'
- C# Linq - Cannot implicitly convert IEnumerable<string> to List<string>
- Cannot convert implicitly a type 'System.Linq.IQueryable' into 'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable'
- Cannot implicitly convert type 'int' to 'System.Collections.Generic.List<QuickTest.Stock>'
- How to convert Generic List<anonymous type > to Generic List <Classname>?
- List of Interfaces vs. List of Derived Type - Cannot Convert Expression Type to Return Type
- Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.List<modelClass>
- LINQ: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int'
- cannot implicitly convert type in LINQ
- Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'
- Cannot implicitly convert type System.Linq.IQueryable<string> to string
- Cannot convert source type to target type List<KeyValuePair> Linq
- Linq - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' to 'System.Collections.Generic.List<string>'
- Cannot implicitly convert type 'Program.Data.View' TO System.linq.iqueryable<Program.Data.View>.
- Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<Model.tblLaborBankAccount>'
- Cannot implicitly convert type 'System.Collections.Generic.IEnumerable>>' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists
- cannot implicitly convert type when groupby
- WCF Data Service : Many to many query
- get common prefix of two string
- Linq to return records that don't have a follow up record
- LINQ - Joining 3 tables and selecting an item with two lists
- How to compare two tables in linq to sql?
- How to optimize this linq query?
- Linq - distinct with a field must return all fields
- How to make multiple includes more efficient with EF Core 3.0
- LINQ GroupBy confusion
- An expression tree may not contain an assignment operator?
- LINQ - SELECT certain columns
- Read from XML Using Linq and add it to a list containing objects
- Filtering in MVC Razor using LINQ
- How to get the MAX row with a GROUP BY?
- Using LINQ in .Net 3.1 to get all items from a database table that matches a list of ID's
- Formatting within Entity Framework Where Clause
- Groupby multiple date properties by month and year in LINQ
- Grouping by a field in DocumentDB
- Translate from lambda (method) syntax to query syntax (LINQ)
- update xml using linq in c#