In this article we are going to see how to create a parallel query execution and how to do the cross apply in Linq.
Parallel Query Execution :
/* Generate
Even Numbers using parallel Extension. Execution of query in parallel to
generate the Even number */
You can see the output of this program that the even numbers are print in UnOrder manner due to the parallel execution of query.
IEnumerable<int> values = ParallelEnumerable.Range(1,
40).Where(x => x % 2 == 0);
foreach (int i in values)
Console.WriteLine(i);
2
4
22
32
6
12
24
34
8
14
26
36
10
16
28
38
18
30
40
20
Cross Apply:
Some times we have a requirement that cross apply the collection and get the value in another collection.
this can be done by SelectMany of one collection for each value of select in another collection.
/* Cross Apply in
Linq */
var list1 = new List<string>() { "PC",
"LAP", "TAB"
};
var list2 = new List<string>() { "HomeTheatre",
"Graphics Card", "Moderm" };
var cross =
list1.SelectMany(x => list2.Select(y => x + "+"
+ y));
foreach (string crs in cross)
Console.WriteLine(crs);
PC+HomeTheatre
PC+Graphics Card
PC+Moderm
LAP+HomeTheatre
LAP+Graphics Card
LAP+Moderm
TAB+HomeTheatre
TAB+Graphics Card
TAB+Moderm
From this article I hope that you can learn the some basics parallel query execution and how to do the cross apply of Linq.
No comments:
Post a Comment