我在SQL Server中的表的结构是这样的:使用LINQ选择分层数据?
id Name Parent
-- ---- ------
1 foo null
2 bar 1
3 oof null
4 rab 3
.
.
.
我需要从两个相关联行的数据作为一个行中的.NET数据表。我想要的数据表是这样的:
Parent Child
------ -----
foo bar
oof rab
我可以使用下面的查询来实现:
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
不过我很好奇,如果有一个简单的方法来做到这一点使用LINQ? (本店使用LINQ对于大多数数据库访问)
你的LINQ语句这里是一个无操作...为什么不使用LINQ加入? – 2012-03-16 17:26:05