2017-05-20 12 views
0

두 개의 다른 테이블을 조인하는 SQL 데이터베이스 테이블이 있습니다.컬렉션 내의 해시 세트를 XML로 올바르게 내보내는 방법

코스 (표 1), 학생 조인 된 테이블이 같은 StudentCourses 보이는라고

(표 2) : 복합 기본 키 2 열 구성 : StudentID,

CourseID 내가 내 데이터베이스를 추가 EDMX를 통해 내 C# 프로젝트에이 조인 된 테이블이 추가되지 않습니다. 이것은 내 프로그램이 끝날 때까지 OK입니다. 여기에서 StudentCourses.xml이라는 XML 파일을 포함하여 모든 데이터를 XML로 내보내야합니다.

course.Students 속성 (hashset)을 사용하여 각 코스의 학생 목록에 액세스 할 수 있으며 student.Courses 속성 (또는 hashset)을 사용하여 각 학생의 코스 목록에 액세스 할 수 있습니다.

나는 foreach loop으로 둥지에 내보내기 프로세스 내에서 학생들의 목록을 시도하고 있지만, 허용되지 않습니다

XDocument documentStudentCourses = new XDocument(
      new XDeclaration("1.0", "utf-8", "yes"), 
      new XComment("Contents of StudentCourses table in CourseroomScheduler database"), 
      new XElement("StudentCourses", 
      from c in CoursesCollection // a List<Course> object 
      select new XElement("Course", 
      foreach (var student in sc.Students) { new XElement("StudentID", student.StudentID); }, // this is not allowed 
      new XElement("CourseID", sc.CourseID)))); 

이에서 XML로 내보내는 hashset에 액세스하는 가장 좋은 방법은 무엇입니까 방법?

+0

'foreach (sc 학생의 var 학생)'을'sc 학생의 from 학생 '으로 바꾸어 보았습니까? –

+0

xml의 루트 요소는 배열이 될 수 없습니다. 그래서 간단히 루트 요소를 만들고 루트에 배열을 넣으십시오. XElement 루트 = 새 XElement ("루트"); root.Add ("documentStudentCourses"); – jdweng

+1

의견을 보내 주신 모든 분들께 감사드립니다! – zig

답변

0

나는 당신이 원하는 것을 다음과 같이 믿습니다.

var studentCourses = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"), 
    new XComment("Contents of StudentCourses table in CourseroomScheduler database"), 
    new XElement("StudentCourses", 
     from c in CoursesCollection 
     select new XElement("Course", from student in c.Students 
             select new XElement("StudentID", student.StudentID)) 
    ) 
); 

하지만 해시 츠와 관련해서는 다른 어떤 종류의 것보다 많지 않습니다.

+0

이것은 정확히 내가 필요로하는 것입니다. 이 컨텍스트에서 구문을 작성하는 방법을 알아 내는데 어려움을 겪고있었습니다. 정말 고맙습니다! – zig