Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested 'froms' in LINQ

Tags:

I'm new in LINQ and I have problem with nested froms:

using System;
using System.Linq;
class MultipleFroms
{
    static void Main()
    {
        char[] chrs = { 'A', 'B', 'C'};
        char[] chrs2 = { 'X', 'Y', 'Z' };
        var pairs = from ch1 in chrs
                    from ch2 in chrs2
                    select ch1+" "+ ch2;
        Console.WriteLine("For ABC and XYZ: ");
        foreach (var p in pairs)
            Console.WriteLine(p);
        Console.WriteLine();

        Console.WriteLine("For D and W: ");
        chrs = new char[] { 'D' };
        chrs2 = new char[] { 'W' };
        foreach (var p in pairs)
            Console.WriteLine(p);
    }
}

In output I have:

For ABC and XYZ:
A X
A Y
A Z
B X
B Y
B Z
C X
C Y
C Z

For D and W:
A W
B W
C W

But I expected:

...
For D and W:
D W

Why pairs in second case used "old" chrs, { 'A', 'B', 'C'} instead {'D'} ?


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!