linq对象图导航逗号分隔字符串

时间:2016-10-07 00:52:05

标签: c# linq dapper comma

  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列表并存储在列表中的patientViewModel.Patient_VisitHistoryViewModelList.visitstudylist ...

我已经设法使用此代码

字段使用了相应的字段patientViewModel.Patient_insurancecompaniesViewModelList
patientViewModel.Patient_insurancecompaniesViewModelList.ToList().ForEach(x => x.insurancecompanyname = ListOfInsuranceCompany.Where(b => x.INSID == b.INSID).SingleOrDefault().insurancecompanyname);

所以我要做的是: 枚举Patient_VisitHistoryViewModelList,获取每次访问,并为每次访问获取每个Visit_studies执行,并为每个Vist_studyperformed获取每个study.studydescription ....

然后以某种方式制作逗号分隔的字符串并将其存储在每个patientViewModel.Patient_insurancecompaniesViewModelList.visitstudylist

我确实遇到过在这里制作逗号分隔列表的方法AutoMapper: Collection to Single string Property,但我遇到了问题。

2 个答案:

答案 0 :(得分:0)

您需要一个字符串数组才能使用Join,而不是Single。尝试这样的事情:string results = String.Join(“,”,patientViewModel.Patient_insurancecompaniesViewModelList.ToList()。ForEach(x =&gt; x.insurancecompanyname = ListOfInsuranceCompany.Where(b =&gt; x.INSID == b.INSID )。选择(y =&gt; y.insurancecompanyname)。ToArray());

答案 1 :(得分: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   )  )  );

以上是解决方案。