Advertisement
javitolin

C# Join Example

Dec 11th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. /*
  2.              * הרעיון של הפונקציה הזאת היא לאחד בין שלושת הטבלאות
  3.              * Courses, ProfessorOfCourses, Persons
  4.              * כדי שבסופו של דבר נוכל להראות את שם הקורס עם השם הפרטי
  5.              * ושם המשפחה של הפרופסור
  6.              */
  7.  
  8.             //אלו הטבלאות
  9.  
  10.             /*
  11.              * Courses:
  12.              * ID,name,points
  13.              *
  14.              * Persons:
  15.              * ID, firstName, lastName, DOB
  16.              *
  17.              * ProfessorsOfCourses:
  18.              * CourseID, ProfesorID
  19.              *
  20.              * StudentsInCourses:
  21.              * CourseID, StudentID
  22.              *
  23.              */
  24.  
  25.             /*
  26.              * ProfInCourses יכיל רשימה של זוגות:
  27.              * PROF_ID, COURSE_NAME. זה בעצם חצי עבודה שרצינו.
  28.              * עשינו ג'וין בין הטבלה Courses לבין הטבלה ProfessorOfCourses
  29.              */
  30.             var profInCourses = from prof in db.ProfessorsOfCourses //עבור כל שורה ב ProfessorsOfCourses
  31.                                 join cour in db.Courses // עושים ג'וין עם הטבלה של קורסים
  32.                                 on prof.CourseID equals cour.ID into profGroup // בוחרים על פי איזה שדה להשוות ומכניסים לטבלה זמנית
  33.                                 from cour2 in profGroup //עוברים על כל השדות בטבלה
  34.                                 select new { PROF_ID = prof.ProfesorID, COURSE_NAME = cour2.name }; //בוחרים אילו שדות נרצה להראות
  35.  
  36.  
  37.             //פה עושים את אותו הדבר שעשינו למעלה
  38.             //אבל עושים זאת עם הטבלאות Person ועם הטבלה שיצרנו למעלה profInCourses.
  39.  
  40.             var profNames = from prof in db.Persons
  41.                             join courProf in profInCourses
  42.                             on prof.ID equals courProf.PROF_ID into coursesGroup
  43.                             from cours in coursesGroup
  44.                             select new {
  45.                                 PROF_NAME = prof.firstName,
  46.                                 PROF_LAST = prof.lastName,
  47.                                 COURSE_NAME = cours.COURSE_NAME
  48.                             };
  49.  
  50.             //מציגים את התוצאה ב DataGrid.
  51.             dataShow.DataSource = profNames;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement