Map a hierarchy of nested DTO objects to a list of business objects Consider I download my data into this data structures:

public class AThingDTO
{
   int AID { get; set; }
   string AName { get; set;}
   Dictionary&lt;int, BThingDTO&gt; BThingsColl { get; set;} 
}

public class BThingDTO
{
   int BID { get; set; }
   string BName { get; set;}
   int AID { get; set; }
   List&lt;CThingDTO&gt; CThingsColl { get; set;}
}

public class CThingDTO
{
   int CID { get; set; }
   string CName { get; set;}
   int BID { get; set; }
   int AID { get; set; }
}


Consider I want to map it into this data structures:

public class AThing
{
   int AID { get; set; }
   string AName { get; set;}
   List&lt;BThing&gt; BThingsColl { get; set; } = new List&lt;BThing&gt;();
}

public class BThing
{
   int BID { get; set; }
   string BName { get; set;}
   List&lt;CThing&gt; CThingsColl { get; set;} = new List&lt;CThing&gt;();
}

public class CThing
{
   int CID { get; set; }
   string CName { get; set;}
}


I did like this:

public List&lt;AThing&gt; MapFromDTO(Dictionary&lt;int, AThingDTO&gt; dtoThingsHierarchy)
{
    var mappedThings = new List&lt;AThing&gt;();

    foreach (var aThingDto in dtoThingsHierarchy)
    {
        var aThing = new AThing();
        aThing.AID = aThingDto.AID;
        aThing.AName = aThingDto.AName;

        foreach (var bThingDto in BThingsColl.Values)
        {
            var bThing = new BThing();
            bThing.BID = bThingDto.BID;
            bThing.BName = bThingDto.BName;

            foreach (var cThingDto in CThingsColl)
            {
                var cThing = new CThing();
                cThing.CID = cThingDto.CID;
                cThing.CName = cThingDto.CName;

                bThing.CThingsColl.Add(cThing);
            }

            aThing.BThingsColl.Add(bThing);

        }

        mappedThings.Add(aThing);

    }

    return mappedThings;
}


Is there a more elegant way / more expressive way to express this mapping? Maybe with LINQ?
The first thing you can do is assign all the properties for a thing in one line using an object initializer.  The following:

var cThing = new CThing();
cThing.CID = cThingDto.CID;
cThing.CName = cThingDto.CName;


would become:

var cThing = new CThing {CID = cThingDto.CID, CName = cThingDto.CName};


You can create a list before creating the parent thing so you can assign in the object initializer.  I don't think your code works currently because you're not specifying the DTO in the foreach loops and not getting the values from the dictionary that's passed in.  If the object initializer is too long, you can expand it to multiple lines or create and use a constructor for each class.

public List&lt;AThing&gt; MapFromDTO(Dictionary&lt;int, AThingDTO&gt; dtoThingsHierarchy)
{
    var mappedThings = new List&lt;AThing&gt;();

    foreach (var aThingDto in dtoThingsHierarchy.Values)
    {           
        var aThing = new AThing {AID = aThingDto.AID, AName = aThingDto.AName};
        foreach (var bThingDto in aThingDto.BThingsColl.Values)
        {
            var bThing = new BThing {BID = bThingDto.BID, BName = bThingDto.BName};
            foreach (var cThingDto in bThingDto.CThingsColl)
            {
                var cThing = new CThing {CID = cThingDto.CID, CName = cThingDto.CName};                 
                bThing.CThingsColl.Add(cThing);
            }               
            aThing.BThingsColl.Add(bThing);             
        }           
        mappedThings.Add(aThing);           
    }       
    return mappedThings;
}


Here's one way to rewrite your code using LINQ's method syntax:

public List&lt;AThing&gt; MapFromDTO(Dictionary&lt;int, AThingDTO&gt; dtoThingsHierarchy)
{       
    return dtoThingsHierarchy.Values
        .Select(a =&gt; new AThing {AID = a.AID, AName = a.AName, BThingsColl = a.BThingsColl.Values
            .Select(b =&gt; new BThing{ BID = b.BID, BName = b.BName, CThingsColl = b.CThingsColl
                .Select(c =&gt; new CThing {CID = c.CID, CName = c.CName}).ToList()}).ToList()}).ToList();
}


Here's LINQ's query syntax:

public List&lt;AThing&gt; MapFromDTO(Dictionary&lt;int, AThingDTO&gt; dtoThingsHierarchy)
{       
    return
       (from a in dtoThingsHierarchy.Values
        let bThings = 
           (from b in a.BThingsColl.Values
            let cThings = b.CThingsColl.Select(c =&gt; new CThing {CID = c.CID, CName = c.CName}).ToList()
            select new BThing{ BID = b.BID, BName = b.BName, CThingsColl = cThings }).ToList()
        select new AThing {AID = a.AID, AName = a.AName, BThingsColl = bThings}).ToList();
}


Both LINQ methods are quite a bit shorter, but the tradeoff in this case is they're kind of tough to read.  Moving logic to the constructors could help.