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 8888283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:59:38+00:00 2026-06-14T21:59:38+00:00

How can I map a c# class called Unit which has again a List<Unit>

  • 0

How can I map a c# class called Unit which has again a List<Unit>.

The concrete scenario is a rootUnit object which contains a List which are the first level children.

The first level children unit objects will not contain any other units so there will be no recursion in the hierarchy.

public class Unit
    {
        public Unit()
        {
            // Explicitly set the default value for the first unit in a hierarchy
            HierarchyIndex = 0;
            Units = new List<Unit>();
        }

        public List<Unit> Units { get; set; }

        public int UnitId { get; set; }
        public string Name { get; set; }       
        public Nullable<int> ParentId { get; set; }
        public int TemplateId { get; set; }       
        public bool HasChildren { get; set; }
        public bool IsFolder { get; set; }
        public DateTime CreatedAt { get; set; }
        public int HierarchyIndex { get; set; }
    }

Map the unit above to this viewmodel:

public class UnitTreeViewModel
{
    [JsonProperty("key")]
    public int UnitId { get; set; }
    [JsonProperty("title")]
    public string Name { get; set; }
    [JsonProperty("isLazy")]
    public bool HasChildren { get; set; } 
    [JsonProperty("isFolder")]
    public bool IsFolder { get; set; } 
}
  • 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-14T21:59:38+00:00Added an answer on June 14, 2026 at 9:59 pm

    Assuming the answer to my question in the comment above is yes, then you’ll need to apply the mapping several times – similar to this question: AutoMapper and flattening nested arrays

    Something like this might work:

    AutoMapperConfigurator.cs

    namespace StackOverflow.ListUnit
    {
        using AutoMapper;
    
        public class MyProfile : Profile
        {
            public override string ProfileName
            {
                get
                {
                    return "MyProfile";
                }
            }
    
            protected override void Configure()
            {
                Mapper.CreateMap<Unit, UnitTreeViewModel>();
            }
        }
    }
    

    MappingTests.cs

    namespace StackOverflow.ListUnit
    {
        using System.Collections.Generic;
        using System.Linq;
    
        using AutoMapper;
    
        using NUnit.Framework;
    
        [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 unit = new Unit
                    {
                        UnitId = 123,
                        Name = "Stack Overflow Rocks",
                        HasChildren = true,
                        IsFolder = true,
                        Units =
                            new List<Unit>
                                {
                                    new Unit
                                        {
                                            UnitId = 123123,
                                            Name = "I'm the first baby",
                                            HasChildren = false,
                                            IsFolder = false,
                                        },
                                    new Unit
                                        {
                                            UnitId = 123321,
                                            Name = "I'm the second baby",
                                            HasChildren = false,
                                            IsFolder = false,
                                        }
                                }
                    };
    
                var unitViewModels = new List<UnitTreeViewModel>
                    {
                        Mapper.Map<Unit, UnitTreeViewModel>(unit)
                    };
                unitViewModels.AddRange(
                    unit.Units.Select(Mapper.Map<Unit, UnitTreeViewModel>));
    
                Assert.That(unitViewModels, Is.Not.Null);
                Assert.That(unitViewModels.Count(), Is.EqualTo(3));
                var unitViewModel = unitViewModels.First(x => x.UnitId == 123);
                Assert.That(unitViewModel, Is.Not.Null);
                Assert.That(unitViewModel.Name, Is.EqualTo("Stack Overflow Rocks"));
                unitViewModel = unitViewModels.First(x => x.UnitId == 123123);
                Assert.That(unitViewModel, Is.Not.Null);
                Assert.That(unitViewModel.Name, Is.EqualTo("I'm the first baby"));
                unitViewModel = unitViewModels.First(x => x.UnitId == 123321);
                Assert.That(unitViewModel, Is.Not.Null);
                Assert.That(unitViewModel.Name, Is.EqualTo("I'm the second baby"));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a singleton class, that has a map which can be accessed by
I have a library with a class called Recipient which has it's own fluent
Can anyone please explain me the meaning of implementing Map class and how should
Can Automapper map values from a NameValueCollection onto an object, where target.Foo receives the
How can I map the value of an object collection using fluent mapping? I
I have a hashset in a class called myClass that contains 6 strings. I
So lets say I have a class called Post that contains an IList I
In apache.commons.collections there is a class called MapUtils which have these two methods to
I have a custom class called Device which implements the MKAnnotation protocol. In the
So I got a class called ObjectA it has many ObjectsB, the objects got

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.