3

Here is my data transfer object

public class LoadSourceDetail
{
  public string LoadSourceCode { get; set; }
  public string LoadSourceDesc { get; set; }
  public IEnumerable<ReportingEntityDetail> ReportingEntity { get; set; }
}

public class ReportingEntityDetail
{
  public string ReportingEntityCode { get; set; }
  public string ReportingEntityDesc { get; set; }
}

And here is my ViewModel

public class LoadSourceViewModel
{
    #region Construction

        public LoadSourceViewModel ()
        {
        }

        public LoadSourceViewModel(LoadSourceDetail data)
        {
            if (data != null)
            {
                LoadSourceCode = data.LoadSourceCode;
                LoadSourceDesc = data.LoadSourceDesc;
                ReportingEntity = // <-- ?  not sure how to do this 
            };
        }


    #endregion
    public string LoadSourceCode { get; set; }  
    public string LoadSourceDesc { get; set; }
    public IEnumerable<ReportingEntityViewModel> ReportingEntity { get; set; }  
}

public class ReportingEntityViewModel 
{ 
    public string ReportingEntityCode { get; set; }
    public string ReportingEntityDesc { get; set; } 
}

}

I'm not sure how to transfer the data from the LoadSourceDetail ReportingEntity to the LoadSourceViewModel ReportingEntity. I'm trying to transfer data from one IEnumerable to another IEnumerable.

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
Ronald McDonald
  • 2,843
  • 11
  • 49
  • 67

3 Answers3

6

I would use AutoMapper to do this:

https://github.com/AutoMapper/AutoMapper

http://automapper.org/

You can easily map collections, see https://github.com/AutoMapper/AutoMapper/wiki/Lists-and-arrays

It would look something like this:

var viewLoadSources = Mapper.Map<IEnumerable<LoadSourceDetail>, IEnumerable<LoadSourceViewModel>>(loadSources);

If you are using this in an MVC project I usually have an AutoMapper config in the App_Start that sets the configuration i.e. fields that do not match etc.

hutchonoid
  • 32,982
  • 15
  • 99
  • 104
  • I use automapper for all my mappings to and from ViewModels – Peadar Doyle Jun 12 '13 at 13:56
  • thanks for the suggestion, but I'd like to know how to do this "manually" without AutoMapper. – Ronald McDonald Jun 12 '13 at 14:06
  • No problem, for me it would be an old fashioned DTO with a To() and From() method. I'll leave my answer there as this is the way I would do it now. – hutchonoid Jun 12 '13 at 14:09
  • Is there a reason why not to use Automapper?? See this answer for http://stackoverflow.com/questions/14317583/passing-dto-to-my-viewmodels-constructor-to-map-properties and this answer http://stackoverflow.com/questions/5995140/models-viewmodels-dtos-in-mvc-3-application?rq=1 for more – Amitd Jun 12 '13 at 14:11
  • I'm not in charge of this project and I don't get to make those decisions and it was decided by more senior people not to use Automapper – Ronald McDonald Jun 12 '13 at 14:26
1

Without AutoMapper you will have to map each property one by one ,

Something like this :

  LoadSourceDetail obj = FillLoadSourceDetail ();// fill from source or somewhere

     // check for null before
    ReportingEntity = obj.ReportingEntity
                     .Select(x => new ReportingEntityViewModel() 
                        { 
                           ReportingEntityCode  = x.ReportingEntityCode,
                           ReportingEntityDesc  x.ReportingEntityDesc
                         })
                     .ToList(); // here is  'x' is of type ReportingEntityDetail
Amitd
  • 4,769
  • 8
  • 56
  • 82
0

You could point it to the same IEnumerable:

ReportingEntity = data.ReportingEntity;

If you want to make a deep copy, you could use ToList(), or ToArray():

ReportingEntity = data.ReportingEntity.ToList();

That will materialize the IEnumerable and store a snapshot in your view model.

Andomar
  • 232,371
  • 49
  • 380
  • 404
  • I get an error "Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)" – Ronald McDonald Jun 12 '13 at 13:58