Created
August 12, 2013 21:22
-
-
Save surfmuggle/6215414 to your computer and use it in GitHub Desktop.
How can i pivot every student in the StudentList so that i can databind a List<CourseDetails> to the same xtragrid.
This is the code for a question on stackoverflow: http://stackoverflow.com/questions/18196882/binding-custom-object-with-list-to-grid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Linqpad demo | |
void Main() | |
{ | |
var getData = new GetData(); | |
getData.Course1().Dump(); | |
getData.Course2().Dump(); | |
var list1 = GetData.StudentList1(); | |
var list2 = GetData.StudentList2(); | |
// List<Student> list1 = GetData.StudentList1().ToList<Student>(); | |
// List<Student> list2 = GetData.StudentList2().ToList<Student>(); | |
list1 = list2.ToList().Union(list1.ToList()).ToList(); | |
list1.Dump(); | |
GetData.GetTuple().Dump(); | |
} | |
public class GetData{ | |
public CourseDetails Course1() { | |
return new CourseDetails(){ | |
Id =1 | |
,Course = new Course(){CourseId = 201, CourseName="SQL Basics"} | |
,Teacher = new Teacher(){TeacherId=30, TeacherName="M Gra"} | |
,Room = new Room(){RoomId=80, RoomName="2th floor R100"} | |
,StudentList = GetData.StudentList2() | |
}; | |
} | |
public CourseDetails Course2(){ | |
return new CourseDetails(){ | |
Id =1 | |
,Course = new Course(){CourseId = 435, CourseName="C# Ninja"} | |
,Teacher = new Teacher(){TeacherId=48, TeacherName="J Skee"} | |
,Room = new Room(){RoomId=32, RoomName="base floor R001"} | |
,StudentList = GetData.StudentList1() | |
}; | |
} | |
public static List<Student> StudentList1(){ | |
return new List<Student>(){ | |
new Student(){Id = 101, StudentName="Amy"} | |
,new Student(){Id = 104, StudentName="Koothrap"} | |
,new Student(){Id = 105, StudentName="Cooper"} | |
}; | |
} | |
public static List<Student> StudentList2(){ | |
return new List<Student>(){ | |
new Student(){Id = 101, StudentName="Amy"} | |
,new Student(){Id = 102, StudentName="Penny"} | |
}; | |
} | |
public static List<Tuple<string, string, string, string, string, string, string>> GetTuple(){ | |
var header = Tuple.Create("Id", "Course", "Teacher", "Amy", "Penny", "Koothrap", "Cooper"); | |
var row1 = Tuple.Create("201", "SQL Basics", "M Gra", "1", "1", "", ""); | |
var row2 = Tuple.Create("435", "C# Ninja", "J Skee", "1", "", "1", "1"); | |
var list = new List<Tuple<string, string, string, string, string, string, string>>(); | |
list.Add(header); | |
list.Add(row1); | |
list.Add(row2); | |
return list; | |
} | |
} | |
public class CourseDetails{ | |
public int Id{ get; set;} | |
public Course Course{ get; set;} | |
public Teacher Teacher{get; set;} | |
public Room Room{get; set;} | |
public List<Student> StudentList{get; set;} | |
} | |
public class Course{ | |
public int CourseId{ get; set;} | |
public string CourseName{ get; set;} | |
} | |
public class Teacher{ | |
public int TeacherId{ get; set;} | |
public string TeacherName{ get; set;} | |
} | |
public class Room{ | |
public int RoomId{ get; set;} | |
public string RoomName{ get; set;} | |
} | |
public class Student{ | |
public int Id{ get; set;} | |
public string StudentName{ get; set;} | |
public override bool Equals(object other) //note parameter is of type object | |
{ | |
Student t = other as Student; | |
return (t != null) ? Id.Equals(t.Id) : false; | |
} | |
public override int GetHashCode() | |
{ | |
return Id.GetHashCode(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment