Programming With C#: Using Linq, Practice
Programming With C#: Using Linq, Practice
7. Add the ToString method shown in bold in the code that follows to the
Employee class. Types in the .NET Framework use this method when
converting the object to a string representation, such as when
displaying it by using the Console.WriteLine statement.
class Employee
{
...
public override string ToString()
{
return String.Format("Id: {0}, Name: {1} {2},
Dept: {3}", this.Id, this.FirstName, this.LastName,
this.Department);
}
}
Microsoft|edX
Microsoft|edX
11.
In Solution Explorer, right-click the QueryBinaryTree solution,
point to Add, and then click Existing Project. In the Add Existing Project
dialog box, move to the folder Microsoft Press\Visual CSharp Step By
Step\Chapter 21\Windows X\BinaryTree in your Documents folder, click
the BinaryTree project, and then click Open.
The BinaryTree project contains a copy of the enumerable BinaryTree
class that you implemented in Chapter 19.
12.
In Solution Explorer, right-click the QueryBinaryTree project, and
then, on the shortcut menu that opens, click Add Reference. In the
Reference Manager QueryBinaryTree dialog box, in the left pane, click
Solution. In the middle pane, select the BinaryTree project, and then
click OK.
13.
Display the Program.cs file for the QueryBinaryTree project in the
Code and Text Editor window, and verify that the list of using directives
at the top of the file includes the following line of code:
using System.Linq;
14.
Add the following using directive that brings the BinaryTree
namespace into scope to the list at the top of the Program.cs file:
using BinaryTree;
15.
In the doWork method in the Program class, remove the // TODO:
comment and add the following statements shown in bold type to
construct and populate an instance of the BinaryTree class:
static void doWork()
{
Tree<Employee> empTree = new Tree<Employee>( new
Employee { Id = 1, FirstName = "Kim", LastName =
"Abercrombie", Department = "IT" });
empTree.Insert( new Employee { Id = 2, FirstName =
"Jeff", LastName = "Hay", Department = "Marketing" });
empTree.Insert( new Employee { Id = 4, FirstName =
"Charlie", LastName = "Herb", Department = "IT" });
empTree.Insert( new Employee { Id = 6, FirstName =
"Chris", LastName = "Preston", Department = "Sales"});
empTree.Insert( new Employee { Id = 3, FirstName =
"Dave", LastName = "Barnett", Department = "Sales" });
Microsoft|edX
List of departments
Department: IT
Department: Marketing
Department: Sales
Department: IT
Department: Marketing
Department: Sales
Each department occurs twice because there are two employees in
each department. The order of the departments is determined by the
CompareTo method of the Employee class, which uses the Id property
of each employee to sort the data. The first department is for the
employee with the Id value 1, the second department is for the
employee with the Id value 2, and so on.
19.
Press Enter to return to Visual Studio 2013.
20.
In the doWork method in the Program class, modify the
statement that creates the enumerable collection of departments as
shown in bold in the following example:
Microsoft|edX
Microsoft|edX
Microsoft|edX
1. In the doWork method, comment out the statement that generates the
enumerable collection of departments, and replace it with the
equivalent statement shown in bold, using the from and select query
operators:
// var depts = empTree.Select(d => d.Department).Distinct();
var depts = (from d in empTree
select d.Department).Distinct();
// var ITEmployees =
// empTree.Where(e => String.Equals(e.Department, "IT"))
// .Select(emp => emp);
var ITEmployees = from e in empTree
where String.Equals(e.Department, "IT")
select e;
2. Comment out the statement that generates the enumerable collection
grouping employees by department, and replace it with the following
code shown in bold:
// var employeesByDept = empTree.GroupBy(e => e.Department);
var employeesByDept = from e in empTree
group e by e.Department;
3. On the Debug menu, click Start Without Debugging. Verify that the
program displays the same results as before.
List of departments
Department: IT
Department: Marketing
Department: Sales
Employees in the IT department
Id: 1, Name: Kim Abercrombie, Dept: IT
Id: 4, Name: Charlie Herb, Dept: IT
All employees grouped by department
Department: IT
Kim Abercrombie
Charlie Herb
Department: Marketing
Jeff Hay
Tim Litton
Department: Sales
Microsoft|edX
Dave Barnett
Chris Preston
4. Press Enter to return to Visual Studio 2013.
Microsoft|edX