Creating a basic parallel query

In this recipe, we will take a look at creating a basic parallel query by using the AsParallel method of the System.Linq.ParallelEnumerable class.

We are going to create a Console application that initializes a collection of employees, and then queries the employee collection looking for a specific job title.

How to do it…

Now, let's go to Visual Studio and start creating some parallel LINQ queries.

  1. Start a new project using the C# Console Application project template and assign SimplePLINQ as the Solution name.
  2. Add the following using directives to the top of your Program class:
    using System;
    using System.Linq;
  3. First, we need to create an Employee class just below the Program Class. Create an Employee class definition with Id, Title, FirstName, and LastName properties.
    public class Employee
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
  4. Now, in the Main method of the Program class, let's create and initialize an array of employees.
    var employees = newList<Employee>
    {
      new Employee{Id=1, Title="Developer", FirstName="Mark", LastName="Smith"},
      new Employee{Id=2, Title="Director", FirstName="Kate", LastName="Williams"},
      new Employee{Id=3, Title="Manager", FirstName="Karen", LastName="Davis"},
      new Employee{Id=4, Title="Developer", FirstName="Maria", LastName="Santos"},
      new Employee{Id=5, Title="Developer", FirstName="Thomas", LastName="Arnold"},
      new Employee{Id=6, Title="Tester", FirstName="Marcus", LastName="Gomez"},
      new Employee{I =7, Title="IT Engineer", FirstName="Simon", LastName="Clark"},
      new Employee{Id=8, Title="Tester", FirstName="Karmen", LastName="Wright"},
      new Employee{Id=9, Title="Manager", FirstName="William", LastName="Jacobs"},
      new Employee{Id=10, Title="IT Engineer", FirstName="Sam", LastName="Orwell"},
      new Employee{Id=11, Title="Developer", FirstName="Tony", LastName="Meyers"},
      new Employee{Id=12, Title="Developer", FirstName="Karen", LastName="Smith"},
      new Employee{Id=13, Title="Tester", FirstName="Juan", LastName="Rodriguez"},
      new Employee{Id=14, Title="Developer", FirstName="Sanjay", LastName="Bhat"},
      new Employee{Id=15, Title="Manager", FirstName="Abid", LastName="Naseem"}
    };
  5. Next, we will create a parallel LINQ query that selects all employees where their title is Developer.
    var results = from e in employees.AsParallel()
                    where e.Title.Equals("Developer")
                    select e;
    Finally, let's loop through the results and display them to the Console, then wait for user input to exit.
    foreach (var employee in results)
    {
        Console.WriteLine("Id:{0}  Title:{1}  First Name:{2}  Last Name:{3}",
            employee.Id, employee.Title, employee.FirstName, employee.LastName);
    }
    Console.ReadLine();
  6. In Visual Studio 2012, press F5 to run the project. You should see output similar to the following screenshot:
    How to do it…

How it works…

The small collection of employees we created for this example is too small to benefit from parallelizing the query, but the key thing to notice in the example is the use of the AsParallel extension method which binds the query to parallel LINQ, and specifies that the rest of the query should be parallelized if possible.

var results = from e in employees.AsParallel()
                where e.Title.Equals("Developer")
                select e;

The System.Linq.ParallelEnumerable class implements all of the parallel LINQ functionality, and exposes parallel versions of Select, Single, Skip, OrderBy, and so on. All of these methods are extension methods that extend ParallelQuery<TSource>. The AsParallel extension method converts your sequential query based on IEnumerable<T> to a parallel query based on ParallelQuery<T>.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.185.199