2016-10-07 34 views
-1
public class PatientViewModel 
    { 
     public int PID { get; set; } 
     public string patientname { get; set; } 
     public virtual ICollection<Patient_VisitHistoryViewModel> Patient_VisitHistoryViewModelList { get; set; } 
     public virtual ICollection<Patient_insurancecompaniesViewModel> Patient_insurancecompaniesViewModelList { get; set; } 
    } 


public class Patient_VisitHistoryViewModel 
{ 
    public int VID { get; set; } 
    public string visitstudylist { get; set; } 
    public string visttype { get; set; } 
} 

public class Patient_insurancecompaniesViewModel 
{ 
    public int PINSID { get; set; } 
    public int PID { get; set; } 
    public int INSID { get; set; } 
    public string insurancecompanyname { get; set; } 
} 





public class Patient 
{ 
     public int PID { get; set; } // PID (Primary key) 
     public string patientname { get; set; } // patientname (length: 100) 

    // Reverse navigation 
     public virtual ICollection<Patient_insurancecompanies> Patient_insurancecompanies { get; set; } // Patient_insurancecompanies.FK_Patient_insurancecompanies_Patient 
     public virtual ICollection<Visit> Visit { get; set; } // Visit.FK_Visit_Patient 
} 


public class Visit 
{ 
     public int VID { get; set; } // VID (Primary key) 
     public int PID { get; set; } // PID 
     // Reverse navigation 
     public virtual ICollection<Visit_studiesperformed> Visit_studiesperformed { get; set; } // Visit_studiesperformed.FK_visits_studiesperformed_visits 

     // Foreign keys 
     public virtual Branch Branch { get; set; } // FK_visits_branches 
     public virtual Patient Patient { get; set; } // FK_Visit_Patient 
     public virtual Visittype Visittype { get; set; } // FK_Visit_Visittype 
} 



public class Visit_studiesperformed 
{ 
     public int VSPID { get; set; } // VSPID (Primary key) 
     public int VID { get; set; } // VID 
     public int STID { get; set; } // STID 

     // Reverse navigation 
     public virtual ICollection<Visit_studiesperformed_medicalreport> Visit_studiesperformed_medicalreport { get; set; } // Visit_studiesperformed_medicalreport.FK_visits_medicalreport_visits_studiesperformed 

     // Foreign keys 
     public virtual Studies Studies { get; set; } // FK_Visit_studiesperformed_studies 
     public virtual Visit Visit { get; set; } // FK_visits_studiesperformed_visits 
} 


public class Studies 
{ 
     public int STID { get; set; } // STID (Primary key) 
     public string studydescription { get; set; } // studydescription (length: 100) 

     // Reverse navigation 
     public virtual ICollection<Visit_studiesperformed> Visit_studiesperformed { get; set; } // Visit_studiesperformed.FK_Visit_studiesperformed_studies 
} 


public class Insurancecompany 
{ 
     public int INSID { get; set; } // INSID (Primary key) 
     public string insurancecompanyname { get; set; } // insurancecompanyname (length: 100) 

     // Reverse navigation 
     public virtual System.Collections.Generic.ICollection<Patient_insurancecompanies> Patient_insurancecompanies { get; set; } // Patient_insurancecompanies.FK_Patient_insurancecompanies_Insurancecompany 

} 



public ActionResult Edit(int PID) 
{ 

    Patient patient = bLLPatient.GetPatientByPID(PID); 

    List<Insurancecompany> ListOfInsuranceCompany = bLLControltables.GetList_Insurancecompany(); 
    List<Studies> ListofStudies = bLLControltables.GetList_Studies(); 

    PatientViewModel patientViewModel = mapper.Map<PatientViewModel>(patient); 

    patientViewModel.Patient_insurancecompaniesViewModelList.ToList().ForEach(x => x.insurancecompanyname = ListOfInsuranceCompany.Where(b => x.INSID == b.INSID).SingleOrDefault().insurancecompanyname); 

    return View(patientViewModel); 
} 

看看我公開的ActionResult Edit(int PID)的底部。我試圖讓Studies.studydescription的一個逗號分隔的列表,並存儲在INT列表patientViewModel.Patient_VisitHistoryViewModelList.visitstudylist ...linq對象圖導航逗號分隔字符串

我設法得到類似與現場patientViewModel.Patient_insurancecompaniesViewModelList做了一些與此代碼

patientViewModel.Patient_insurancecompaniesViewModelList.ToList().ForEach(x => x.insurancecompanyname = ListOfInsuranceCompany.Where(b => x.INSID == b.INSID).SingleOrDefault().insurancecompanyname); 

所以我要做的就是: 枚舉Patient_VisitHistoryViewModelList,獲得每次訪問和每次訪問得到各Visit_studiesperformed和每個Vist_studyperformed獲取每個studies.studydescription ....

然後以某種方式使一個逗號分隔的字符串,並將其存儲在每個patientViewModel.Patient_insurancecompaniesViewModelList.visitstudylist

我還是設法遇到一個方法,使以逗號分隔的列表在這裏AutoMapper: Collection to Single string Property,但我有把它在一起的問題。

回答

0

您需要一個字符串數組才能使用Join,而不是Single。嘗試像這樣:string results = String.Join(「,」,patientViewModel.Patient_insurancecompaniesViewModelList.ToList()。ForEach(x => x.insurancecompanyname = ListOfInsuranceCompany.Where(b => x.INSID == b.INSID)。選擇(Y => y.insurancecompanyname).ToArray());

+0

這得到我的一半用逗號分隔的字符串,但我仍然有通過對象模型導航問題()= x.visitstudylist = patient.Visit.ToList()。ForEach(y => y.Visit_studiesperformed.ToList()。ForEach (z => z.Studies.studydescription.ToString()));獲取錯誤不能隱含它將void轉換爲字符串.... – rdurant

+0

它看起來像一些屬性爲空。使用ToString()將在項目爲空時提供erorrs。試試這個:string results = String.Join(「,」,patientViewModel.Patient_insurancecompaniesViewModelList.ToList()。ForEach(x => x.insurancecompanyname = ListOfInsuranceCompany.Where(b =>(x.INSID == b.INSID)&& (x.insurancecompanyname!= null))。Select(y =>(string)y.insurancecompanyname).ToArray()); – jdweng

0
 patientViewModel.Patient_VisitHistoryViewModelList.ToList().ForEach(x => x.visitstudylist = String.Join(", ", patient.Visit.Single(b => b.VID == x.VID).Visit_studiesperformed.Where(m => m.status == "Active").ToList().Select(w => ListofStudies.Where(b => w.STID== b.STID).SingleOrDefault().studydescription ) ) ); 

以上是解決