Search code examples
asp.net-mvcvb.netasp.net-mvc-3object3-tier

Convert object list to another object list even they are not same type


I want to be separate between ViewModel and Mode(Data Access Object Or Data Members). Please see below.

Major DAO

Public Class Major

       Public MajorID as Integer
       Public MajorName as String

End Class

Major VM

Public Class MajorVM

       Public Major as Major

End Class

But Major View Model take a reference for major. In this case, I would like to convert major object to majorvm object list. Please see below.

Dim MajorList as list(of Major) = GetAllMajorList()
Dim MajorVMList as List(of MajorVM)

MajorVMList = MajorList

How can I solve this? Thanks.


Solution

  • Try this.

    Dim MajorVMList as New List(of MajorVM)
    
    For each item as Major in MajorList 
    
     Dim m as new Major
     Major.MajorID = item.MajorID
     Major.MajorName = item.MajorName
    
     MajorVMList.Add(Major)
    
    
    Next