Sankara
Sankara

Reputation: 1479

Cross join or full join in LINQ to SQL query

I stumbled upon this LINQ query while reading a book.

var binary = new int[] { 0, 1 };

var q = from b4 in binary
        from b3 in binary
        from b2 in binary
        from b1 in binary
        select String.Format("{0}{1}{2}{3}", b4, b3, b2, b1);

foreach (var element in q)
    Console.WriteLine(element);

The result of the above LINQ will be

0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111

I wanted to see how this will be in SQL Server.

What I have tried:

create table LINQ(x bit)
insert LINQ select 0
insert LINQ select 1

create table LINQ(x bit, y bit)
insert LINQ select 0,1

I tried using LINQPAD (does not give the SQL or lambda version), using the above temp tables. I tried cross join, full join. I did not get the SQL which gives the same result as LINQ.

Upvotes: 0

Views: 1047

Answers (1)

PinnyM
PinnyM

Reputation: 35533

SELECT CAST(l1.x as varchar(4)) + CAST(l2.x as varchar(4))+ CAST(l3.x as varchar(4)) + CAST(l4.x as varchar(4))
FROM LINQ as l1, LINQ as l2, LINQ as l3, LINQ as l4
ORDER BY l1.x, l2.x, l3.x, l4.x

sqlfiddle here

Upvotes: 2

Related Questions