Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 21 INTRODUCTION TO LINQ

What Is a Join?

A join in LINQ takes two collections and creates a new collection where each element has members from the elements of the two original collections.

For example, the following code declares two classes: Student and CourseStudent.

Objects of type Student contain a student’s last name and student ID number.

Objects of type CourseStudent represent a student that is enrolled in a course and contain the course name and a student ID number.

public class Student

{

public

int

StID;

public

string

LastName;

}

public class CourseStudent

{

public

string

CourseName;

public

int

StID;

}

Figure 21-6 shows the situation in a program where there are three students and three courses, and the students are enrolled in various courses. The program has an array called students, of Student objects, and an array called studentsInCourses, of CourseStudent objects, which contains one object for every student enrolled in each course.

Figure 21-6. Students enrolled in various courses

550

CHAPTER 21 INTRODUCTION TO LINQ

Suppose now that you want to get the last name of every student in a particular course. The students array has the last names, and the studentsInCourses array has the course enrollment information. To get the information, you must combine the information in the arrays, based on the student ID field, which is common to objects of both types. You can do this with a join on the StID field.

Figure 21-7 shows how the join works. The left column shows the students array, and the right column shows the studentsInCourses array. If we take the first student record and compare its ID with the student ID in each studentsInCourses object, we find that two of them match, as shown at the top of the center column. If we then do the same with the other two students, we find that the second student is taking one course, and the third student is taking two courses.

The five grayed objects in the middle column represent the join of the two arrays on field StID. Each object contains three fields: the LastName field from the Students class, the CourseName field from the CourseStudent class, and the StID field common to both classes.

Figure 21-7. Two arrays of objects and their join on field StId

551

CHAPTER 21 INTRODUCTION TO LINQ

The following code puts the whole example together. The query finds the last names of all the students taking the history course.

class Program

 

 

 

 

{

 

 

 

 

public class Student {

 

// Declare classes.

public int

StID;

 

 

 

public string

LastName;

 

 

}

 

 

 

 

public class CourseStudent {

 

 

public string

CourseName;

 

 

public int

StID;

 

 

 

}

 

 

// Initialize arrays.

 

 

 

static CourseStudent[]

studentsInCourses = new CourseStudent[] {

new CourseStudent

{ CourseName =

"Art",

StID = 1 },

new CourseStudent

{ CourseName =

"Art",

StID = 2 },

new CourseStudent

{ CourseName =

"History",

StID = 1 },

new CourseStudent

{ CourseName =

"History",

StID = 3 },

new CourseStudent

{ CourseName =

"Physics",

StID = 3 },

};

 

 

 

 

static Student[]

students = new Student[] {

 

new Student

{ StID = 1, LastName

= "Carson"

},

new Student

{ StID = 2, LastName

= "Klassen"

},

new Student

{ StID = 3, LastName

= "Fleming"

},

};

 

 

 

 

static void Main( )

{

//Find the last names of the students taking history. var query = from s in students

join c in studentsInCourses on s.StID equals c.StID where c.CourseName == "History"

select s.LastName;

//Display the names of the students taking history.

foreach (var q in query)

Console.WriteLine("Student taking History: {0}", q);

}

}

This code produces the following output:

Student taking History: Carson

Student taking History: Fleming

552

CHAPTER 21 INTRODUCTION TO LINQ

The from . . . let . . . where Section in the Query Body

The optional from...let...where section is the first section of the query body. It can have any number of any of the three clauses that comprise it—the from clause, the let clause, and the where clause. Figure 21-8 summarizes the syntax of the three clauses.

Figure 21-8. The syntax of the from . . . let . . . where clause

553

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]