Type casting in TPC inheritance

Posted by Mohsen Esmailpour on Stack Overflow See other posts from Stack Overflow or by Mohsen Esmailpour
Published on 2014-08-23T13:15:54Z Indexed on 2014/08/23 16:21 UTC
Read the original article Hit count: 270

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. enter image description here

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 ?

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about inheritance