# Dynamic variable - is that best way to dynamically assign?

Normally I could just declare the 'email' variable as the base class for both of EmailInbound & EmailOutbound, then cast-up to the inherited class. However, I don't think when defining EF codefirst classes you can use inherited classes, only POCOs. I could be wrong. Hence I've used the dynamic keyword. Could I use Object() instead? Some direction appreciated here...

             if (source.ActionTypeId == (int)ActionTypes.EmailInbound  ||
source.ActionTypeId == (int)ActionTypes.EmailOutbound)
{
dynamic email;
if ( source.ActionTypeId == (int)ActionTypes.EmailInbound )
{
email = source.EmailMessage.EmailInbound;
}
else
{
email = source.EmailMessage.EmailOutbound;
}

return new ActionModel
{
ActionTypeId = (ActionTypes)source.ActionTypeId,
Content = email.Body,
Created = email.DateSent,
Subject = email.Subject,
User = Mapper.Map<User, UserModel>(source.EmailMessage.User),
};
}
...


I think it is quite defendable to use dynamic here. I'm not too familiar with EF, but it could very well be that you can't make the objects type-compatible.

Even if you could, I would not do it if it was only to make this piece of code work.

Declaring email as object will not help in any way.

The only alternative I can think of is to have two code paths for both cases:

            if ( source.ActionTypeId == (int)ActionTypes.EmailInbound )
{
email = source.EmailMessage.EmailInbound;
return new ActionModel
{
ActionTypeId = (ActionTypes)source.ActionTypeId,
Content = email.Body,
Created = email.DateSent,
Subject = email.Subject,
User = Mapper.Map<User, UserModel>(source.EmailMessage.User),
};
}
else
{
email = source.EmailMessage.EmailOutbound;
return new ActionModel
{
ActionTypeId = (ActionTypes)source.ActionTypeId,
Content = email.Body,
Created = email.DateSent,

But I think the solution with dynamic is better. You can't catch all errors at compile time. I would reccomend a unit test (two really) so that if anybody changes the database schema, this is detected at build time.