I have an EF object Account that has 4 xrefs off for different portfolio types. For simplicity, I will call these types A, B, C, and D.
public class Account
{
public long AccountId { get; set; }
public PortfolioTypeAXref { get; set; }
public PortfolioTypeBXref { get; set; }
public PortfolioTypeCXref { get; set; }
public PortfolioTypeDXref { get; set; }
}
My destination object is a flattened object.
public class AccountDto
{
public long AccountId { get; set; }
public PortfolioType PortfolioType { get; set; } //this is an enum with values A, B, C, D, Unknown
public long? PortfolioId { get; set; }
}
Each Xref object looks something like this
public class PortfolioTypeAXref
{
public long XrefId { get; set; }
public long AccountId { get; set; }
public long? PortfolioTypeAId { get; set; }
public long? HypotheticalPortfolioTypeAId { get; set; }
}
I am using linq to Ef projection to translate my object. I've also written a custom Value resolver, but when using MapFrom
I noticed it says it is not for projections.
The logic for the resolver essentially checks which (if any) of the xrefs have a portfolio id assigned, and if they do, return an enum with the appropriate value.
profile.CreateMap<Account, AccountDto>()
.ForMember(dest => dest.PortfolioType, opt => opt.MapFrom<PortfolioTypeResolver>())
.ForAllOtherMembers(opt => opt.Ignore());
However, this causes an error, likely because using MapFrom
with a custom resolver isn't meant to work with projections. I'm not married to the idea of using a custom resolver - it could just be a function call, but how do I set the destination property based on custom logic that uses the value of the source object?