Linq Query:
Explanation:
What we are basically doing is that ,first we compute (Number1 X Number2) pairs.
we will have 25 pairs (5*5).
For example: (0,1),(0,3),(0,2) ... and so on..
Now i want to find in which pair the first element is greater than the second.
That 's what this query does it finds the pair ( out of this 25 ) which satisfy the condition.
Output:
We can do the same this with for loops .. see the code below which is equivalent to above linq query.
Without Linq:
This above code will give the Same output as the linq query.
//SelectMany Compound From 1
int[] Numbers1 = { 0, 2, 4, 6, 8 };
int[] Numbers2 = { 1, 3, 5, 7, 9 };
var myPair =
from n1 in Numbers1
from n2 in Numbers2
where n1 > n2
select new { n1, n2 };
foreach (var t in myPair)
{
Console.WriteLine(t.n1.ToString() +" - " + t.n2.ToString());
}
Explanation:
What we are basically doing is that ,first we compute (Number1 X Number2) pairs.
we will have 25 pairs (5*5).
For example: (0,1),(0,3),(0,2) ... and so on..
Now i want to find in which pair the first element is greater than the second.
That 's what this query does it finds the pair ( out of this 25 ) which satisfy the condition.
Output:
2 - 1
4 - 1
4 - 3
6 - 1
6 - 3
6 - 5
8 - 1
8 - 3
8 - 5
8 - 7
2 - 1
4 - 1
4 - 3
6 - 1
6 - 3
6 - 5
8 - 1
8 - 3
8 - 5
8 - 7
We can do the same this with for loops .. see the code below which is equivalent to above linq query.
Without Linq:
for (int i = 0; i < Numbers1.Length; i++)
{
for (int j = 0; j < Numbers2.Length; j++)
{
if (Numbers1[i] > Numbers2[j])
{
Console.WriteLine(Numbers1[i] + " - " + Numbers2[j]);
}
}
}
This above code will give the Same output as the linq query.
0 comments:
Post a Comment