-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParallelLinqExample.cs
34 lines (32 loc) · 1.09 KB
/
ParallelLinqExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System.Linq;
namespace DotNetAsync.ParallelLinq
{
public class ParallelLinqExample
{
private const string Alphabet = @"abcdefghijklmnopqrstuvwxyz";
private readonly Random _random = new Random();
public void Run()
{
Console.WriteLine("Generating random text...");
var lines = Enumerable.Range(1, 1000000)
.AsParallel()
.Select(number =>
{
var lineLength = _random.Next(10, 100);
var line = Enumerable.Range(1, lineLength)
.Select(_ => Alphabet[_random.Next(Alphabet.Length)])
.ToArray();
return new
{
Index = number,
Text = new string(line)
};
});
Console.WriteLine("Printing first 10 lines.");
lines.Take(10)
.ToList()
.ForEach(line => Console.WriteLine("{0:##} {1}", line.Index, line.Text));
}
}
}