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.
Now, let's go to Visual Studio and start creating some parallel LINQ queries.
SimplePLINQ
as the Solution name.using
directives to the top of your Program
class:using System; using System.Linq;
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; } }
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"} };
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();
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>
.
13.58.185.199