1

I have an EF data structure which has data nested two levels deep, it looks roughly like this:

class Country {
    string Name {get; set;}
    ICollection<State> States {get; set;}
}

class State {
    string Name {get; set;}
    ICollection<County> Counties {get; set;}
}

class County {
     string Name {get; set;}
}

Or: a country has states, and each state has counties.

I've simplified much of the remote facade for this example so my DTO models are effectively the same structure here. Using ValueInjector I'd like my code to look something like.

 var dbCountry = _dbContext.Countries.Find(id);
 var dtoCountry = new DTO.Country();
 dtoCountry.InjectFrom<FlatLoopInjection>(dbCountry);
 foreach (var dbState in dbCountry.States) {
    var dtoState= new DTO.State();
    dtoState.InjectFrom(dbState);
    dtoState.Counties = dbState
       .Counties
       .Select(m => new DTO.County().InjectFrom(m))
       .Cast<DTO.County>()
       .ToList();
    dtoCountry.States.Add(dtoState);
 }

The question is: is there a way to to this without the foreach loop? Not that I'm anti-loop (and I'd love to start a holy war on this with the trolls), but this seems to mixing two patterns together (linq and loop).

Thanks.

THBBFT
  • 1,161
  • 1
  • 13
  • 29
  • have a read of this: https://github.com/omuleanu/ValueInjecter/blob/master/README.md, you can put the loop inside the `AddMap` – Omu Jun 01 '16 at 03:52

0 Answers0