Newbie in LINQ - Just a quick question
-
Hi all, I am tryin go run the query based on two lists, where the one list matches the other list. In the example below,I'm trying to match the teacher with the student with regards to their subjects. I am struggling ... Can anyone please help ??
class Student { public string First { get; set; } public string Last { get; set; } public string Subject { get; set; } public string Street { get; set; } public string City { get; set; } public List<int> Scores; } class Teacher { public string First { get; set; } public string Last { get; set; } public string Subject { get; set; } public string City { get; set; } } ... List<Student> students = new List<Student>() { new Student {First="Svetlana", Last="Omelchenko", Subject="English", Scores= new List<int> {97, 92, 81, 60}}, new Student {First="Claire", Last="O’Donnell", Subject="Biology", Scores= new List<int> {75, 84, 91, 39}}, new Student {First="Sven", Last="Mortensen", Subject="Afrikaans", Scores= new List<int> {88, 94, 65, 91}}, new Student {First="Cesar", Last="Garcia", Subject="Science", Scores= new List<int> {97, 89, 85, 82}}, new Student {First="Debra", Last="Garcia", Subject="Earch Sience", Scores= new List<int> {35, 72, 91, 70}}, new Student {First="Fadi", Last="Fakhouri", Subject="English", Scores= new List<int> {99, 86, 90, 94}}, new Student {First="Hanying", Last="Feng", Subject="German", Scores= new List<int> {93, 92, 80, 87}}, new Student {First="Hugo", Last="Garcia", Subject="Programming", Scores= new List<int> {92, 90, 83, 78}}, new Student {First="Lance", Last="Tucker", Subject="Social Studies", Scores= new List<int> {68, 79, 88, 92}}, new Student {First="Terry", Last="Adams", Subject="Science", Scores= new List<int> {99, 82, 81, 79}}, new Student {First="Eugene", Last="Zabokritski", Subject="Afrikaans", Scores= new List<int> {96, 85, 91, 60}}, new Student {First="Michael", Last="Tucker", Subject="Zulu", Scores= new List<int> {94, 92, 91, 91} }, }; // Create the second data source. List<Teacher> teachers = new List<Teacher>() { new Teacher {First="Ann", Last="Beebe", Subject="Af
-
Hi all, I am tryin go run the query based on two lists, where the one list matches the other list. In the example below,I'm trying to match the teacher with the student with regards to their subjects. I am struggling ... Can anyone please help ??
class Student { public string First { get; set; } public string Last { get; set; } public string Subject { get; set; } public string Street { get; set; } public string City { get; set; } public List<int> Scores; } class Teacher { public string First { get; set; } public string Last { get; set; } public string Subject { get; set; } public string City { get; set; } } ... List<Student> students = new List<Student>() { new Student {First="Svetlana", Last="Omelchenko", Subject="English", Scores= new List<int> {97, 92, 81, 60}}, new Student {First="Claire", Last="O’Donnell", Subject="Biology", Scores= new List<int> {75, 84, 91, 39}}, new Student {First="Sven", Last="Mortensen", Subject="Afrikaans", Scores= new List<int> {88, 94, 65, 91}}, new Student {First="Cesar", Last="Garcia", Subject="Science", Scores= new List<int> {97, 89, 85, 82}}, new Student {First="Debra", Last="Garcia", Subject="Earch Sience", Scores= new List<int> {35, 72, 91, 70}}, new Student {First="Fadi", Last="Fakhouri", Subject="English", Scores= new List<int> {99, 86, 90, 94}}, new Student {First="Hanying", Last="Feng", Subject="German", Scores= new List<int> {93, 92, 80, 87}}, new Student {First="Hugo", Last="Garcia", Subject="Programming", Scores= new List<int> {92, 90, 83, 78}}, new Student {First="Lance", Last="Tucker", Subject="Social Studies", Scores= new List<int> {68, 79, 88, 92}}, new Student {First="Terry", Last="Adams", Subject="Science", Scores= new List<int> {99, 82, 81, 79}}, new Student {First="Eugene", Last="Zabokritski", Subject="Afrikaans", Scores= new List<int> {96, 85, 91, 60}}, new Student {First="Michael", Last="Tucker", Subject="Zulu", Scores= new List<int> {94, 92, 91, 91} }, }; // Create the second data source. List<Teacher> teachers = new List<Teacher>() { new Teacher {First="Ann", Last="Beebe", Subject="Af
It would have been helpful if you explained exactly what you wanted but is this the kind of thing you're after?
var results = from s in students
where teachers.FirstOrDefault(t => (t.Subject == s.Subject)) != null
select s.Last + " " + s.First + " MARK: " + s.Scores[0];
I doubt it. If it isn't intuitive then we need to fix it. - Chris Maunder