Type casting in TPC inheritance
- by Mohsen Esmailpour
I have several products like HotelProduct, FlightProduct ... which derived from BaseProduct class. The table of these products will be generated in TPC manner in database. There is OrderLine class which has a BaseProduct.
My problem is when i select an OrderLine with related product i don't know how cast BaseProduct to derived product. for example i have this query:
var order = (from odr in _context.Orders
join orderLine in _context.OrderLines on odr.Id equals orderLine.OrderId
join hotel in _context.Products.OfType<HotelProduct>() on orderLine.ProductId equals hotel.Id
where odr.UserId == userId && odr.Id == orderId
orderby odr.OrderDate descending
select odr).SingleOrDefault();
In OrderLine i have BaseProduct properties not properties of HotelProduct. Is there any way to cast BaseProduct to derived class in OrderLine or any other solutions ?