score:1

Simple SelectMany should be enough to flatten the collection:

customer.ContentTypePreferences = source.RequestCustomer.MarketingPreferences
    .SelectMany(mp => mp.ContentTypePreferences)
    .Select(contentPrefs => new ContentTypePreferences
            {
                TypeId = contentPrefs.Type,
                OptIn = contentPrefs.OptIn,
                ValidFromDate = contentPrefs.ValidFromDate
            })
    .ToList();

score:1

I did find a way to solve this issue using the below.

    customer.MarketingPreferences = source.RequestCustomer.MarketingPreferences
        .Select(x => new MarketingPreferences()
        {
            ChannelId = x.Channel,
            OptIn = x.OptIn,
            ValidFromDate = x.ValidFromDate,
            UpdatedBy = Functions.DbUser,
            CreatedDate = DateTime.UtcNow,
            UpdatedDate = DateTime.UtcNow,
            ContentTypePreferences = (from c in x.ContentTypePreferences
                    select new ContentTypePreferences
                    {
                        TypeId = c.Type,
                        OptIn = c.OptIn,
                        ValidFromDate = c.ValidFromDate,
                        ChannelId = x.Channel // Should inherit parent marketing preference channel
                    }).ToList(),
            UpdatingStore = null // Not passed by API
        })
        .ToList();

I appreciate the other answers, but realised that just flattening might not be enough, as the ChannelId for each ContentTypePreference isn't passed in with the POST JSON but is instead derived from the parent MarketingTypePreference for that ContentTypePreference (e.g. a child of EMAIL will have a channel Id of EMAIL) so the above solution lets me inject that when creating each new ContentTypePreferences for Entity Framework.


Related Articles