I have a table in SQL Server that is structured as follows:
id Name Parent
-- ---- ------
1 foo null
2 bar 1
3 oof null
4 rab 3
.
.
.
I need to get data from two related rows as one row in a .NET DataTable. My desired DataTable would look like this:
Parent Child
------ -----
foo bar
oof rab
I was able to accomplish this using the following query:
with temp as
(
SELECT 1 id,'foo' name, null parent
UNION
select 2,'bar', 1
UNION
SELECT 3,'oof', null
UNION
select 4,'rab', 3
)
SELECT t1.name parent, t2.name child
FROM temp t1
INNER JOIN temp t2
ON t1.id = t2.parent
But I'm curious if there is an easy way to do this using LINQ? (our store uses LINQ for most database access)
source
share