GroupJoin 操做

GroupJoinspa

The GroupJoin operator performs the same task as Join operator except that GroupJoin returns a result in group based on specified group key.
The GroupJoin operator joins two sequences based on key and groups the result by matching key and then returns the collection of grouped result and key.code

Exampleorm

  1. publicclassStudent{
  2. publicintStudentID{get;set;}
  3. publicstringStudentName{get;set;}
  4. publicintStandardID{get;set;}
  5. }
  6. publicclassStandard{
  7. publicintStandardID{get;set;}
  8. publicstringStandardName{get;set;}
  9. }
  10. IList<Student> studentList =newList<Student>(){
  11. newStudent(){StudentID=1,StudentName="John",StandardID=1},
  12. newStudent(){StudentID=2,StudentName="Moin",StandardID=1},
  13. newStudent(){StudentID=3,StudentName="Bill",StandardID=2},
  14. newStudent(){StudentID=4,StudentName="Ram",StandardID=2},
  15. newStudent(){StudentID=5,StudentName="Ron"}
  16. };
  17. IList<Standard> standardList =newList<Standard>(){
  18. newStandard(){StandardID=1,StandardName="Standard 1"},
  19. newStandard(){StandardID=2,StandardName="Standard 2"},
  20. newStandard(){StandardID=3,StandardName="Standard 3"}
  21. };
  22. var groupJoin = standardList.GroupJoin(studentList,//inner sequence
  23. std => std.StandardID,//outerKeySelector
  24. s => s.StandardID,//innerKeySelector
  25. (std, studentsGroup)=>new// resultSelector
  26. {
  27. Students= studentsGroup,
  28. StandarFulldName= std.StandardName
  29. });
  30. foreach(var item in groupJoin)
  31. {
  32. Console.WriteLine(item.StandarFulldName);
  33. foreach(var stud in item.Students)
  34. Console.WriteLine(stud.StudentName);
  35. }
  36. // Results
  37. Standard1:
  38. John,
  39. Moin,
  40. Standard2:
  41. Bill,
  42. Ram,
  43. Standard3:
相關文章
相關標籤/搜索