Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8913599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:30:08+00:00 2026-06-15T04:30:08+00:00

I have a Domain model that countains the following properties : public class Profile

  • 0

I have a Domain model that countains the following properties :

public class Profile
{
    public DateTime birthDate { get; set; }
    public List<Language> languages { get; set; }
}

Where Language represent an enumeration defined here :

public enum Language
{
    English,
    French,
    Spannish
}

i would like to automatically populate the two following properties based on the languages values stored inside my domain model :

public Dictionary <int, String> languages_list { get; set; }
public List<string> languages_known { get; set; }

the question is : can it be done using automapper , if so how should i proceed ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:30:09+00:00Added an answer on June 15, 2026 at 4:30 am

    OK, using the detail you’ve provided this is what I’ve come up with.

    Classes

    public class SOProfile
    {
        public DateTime birthDate { get; set; }
        public List<Language> languages { get; set; }
    }
    
    public class WhatAmI
    {
        public Dictionary<int, String> languages_list { get; set; }
        public List<string> languages_known { get; set; }
    }
    

    Note that Profile was renamed to SOProfile to avoid a clash with AutoMapper

    AutoMapper Configuration

    public class MyProfile : Profile
    {
        public override string ProfileName
        {
            get
            {
                return "MyProfile";
            }
        }
    
        protected override void Configure()
        {
            Mapper.CreateMap<SOProfile, WhatAmI>()
                  .ForMember(dest => dest.languages_list,
                             opt => opt.MapFrom(
                                 src => src.languages
                                     .Select((x,i) => new { Item = x, Index = i})
                                     .ToDictionary(x => x.Index,
                                                   x => x.Item.ToString())))
                  .ForMember(dest => dest.languages_known,
                             opt => opt.MapFrom(
                                 src => src.languages
                                     .Select(x => x.ToString()).ToList()));
        }
    }
    

    Unit Tests

    [TestFixture]
    public class MappingTests
    {
        [Test]
        public void AutoMapper_Configuration_IsValid()
        {
            Mapper.Initialize(m => m.AddProfile<MyProfile>());
            Mapper.AssertConfigurationIsValid();
        }
    
        [Test]
        public void AutoMapper_Mapping_IsValid()
        {
            Mapper.Initialize(m => m.AddProfile<MyProfile>());
            Mapper.AssertConfigurationIsValid();
    
            var profile = new SOProfile
                {
                    birthDate = new DateTime(2012, 01, 01),
                    languages = new List<Language>
                        {
                            Language.English,
                            Language.French,
                            Language.English,
                            Language.French
                        }
                };
    
            var rc = Mapper.Map<SOProfile, WhatAmI>(profile);
    
            Assert.That(rc, Is.Not.Null);
    
            Assert.That(rc.languages_known, Is.Not.Null);
            Assert.That(rc.languages_known.Count, Is.EqualTo(4));
    
            Assert.That(rc.languages_known.Count(x => x == "English"),
                        Is.EqualTo(2));
            Assert.That(rc.languages_known.Count(x => x == "French"),
                        Is.EqualTo(2));
            Assert.That(rc.languages_known.Count(x => x == "Spanish"),
                        Is.EqualTo(0));
    
            Assert.That(rc.languages_list, Is.Not.Null);
            Assert.That(rc.languages_list.Count, Is.EqualTo(4));
    
            Assert.That(rc.languages_list.First(x => x.Key == 0).Value,
                        Is.EqualTo("English"));
            Assert.That(rc.languages_list.First(x => x.Key == 1).Value,
                        Is.EqualTo("French"));
            Assert.That(rc.languages_list.First(x => x.Key == 2).Value,
                        Is.EqualTo("English"));
            Assert.That(rc.languages_list.First(x => x.Key == 3).Value,
                        Is.EqualTo("French"));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class in my domain model root that looks like this: namespace
I have the following domain model public interface IAppliedTo { public Guid Id {
I have a Domain Model for Countries and States that looks like the following(see
I have a partial view which contains the following snippet: @model Mbrrace.Domain.MbrraceForm <div class=row>
I have the following domain entities: class Customer { // Customer properties... static belongsTo
Let's say i have a Domain Model that im trying to make compatible with
I have two tables that are considered a single entity in my domain model.
I have a form that handles editing of a domain model (A). This domain
I currently have a client that wants me to 'abstract' out a domain model
In Grails 1.3.7, I have a domain model like this: abstract class A {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.