Last post Dec 24, 2015 06:48 AM by Candice Zhou
Dec 23, 2015 01:18 PM|Emwat_Oon|LINK
I have a List<base class> and I want to make it into a List<derived class>
How would I do that? I understand that you cannot convert a base class into a derived class and have to manually code in the constructor.
I pull in the base class using dapper, but I can't do inner joins on the query that pulls in the base class, because the derived class is taking from a different database.
Dec 23, 2015 01:42 PM|Rion Williams|LINK
You might be able to use LINQ to determine if your target class is of the type that you are looking for and if so, then cast it :
var derived = base.Where(x => x is YourDerivedClass)
.Select(x => x as YourDerivedClass)
Dec 23, 2015 02:51 PM|Emwat_Oon|LINK
How would I add derived class properties in that LINQ?
Dec 24, 2015 06:48 AM|Candice Zhou|LINK
You could use an object initializer to set the properties to the Derived class:
List<Derived> list = (from b in db.Bases
let propertyB = SomeCalculation()
select new Derived
Id = b.Id,
PropertyA = b.PropertyA,
PropertyB = propertyB