--
构造测试数据: 只作演示用
CREATE
TABLE
[
dbo
]
.
[
Tim_LinqTable
]
(
[
Id
]
int
PRIMARY
KEY
IDENTITY
(
1
,
1
)
NOT
NULL
,
[
Name
]
[
varchar
]
(
50
)
NOT
NULL
,
[
Parent
]
int
NOT
NULL
,
)
GO
INSERT
INTO
[
Tim_LinqTable
]
SELECT
'
A
'
,
0
UNION
ALL
SELECT
'
A1
'
,
1
UNION
ALL
SELECT
'
A2
'
,
1
UNION
ALL
SELECT
'
B1
'
,
2
UNION
ALL
SELECT
'
B2
'
,
3
UNION
ALL
SELECT
'
C1
'
,
4
UNION
ALL
SELECT
'
C2
'
,
4
UNION
ALL
SELECT
'
D1
'
,
5
UNION
ALL
SELECT
'
D2
'
,
5
UNION
ALL
SELECT
'
D3
'
,
5
GO
WITH
temp
AS
(
SELECT
*
FROM
[
Tim_LinqTable
]
WHERE
Parent
=
3
UNION
ALL
SELECT
m.
*
FROM
[
Tim_LinqTable
]
AS
m
INNER
JOIN
temp
AS
child
ON
m.Parent
=
child.Id
)
SELECT
*
FROM
temp
GO
--
查询 Parent=3 的所有子数据结果如下:
Id Name Parent
--
--------- -------------------------------------------------- -----------
5
B2
3
8
D1
5
9
D2
5
10
D3
5
(
4
row(s) affected)