|
遵循以下两个先前的答案并进行调整以获取所需的结果,我已经成功地在SQL Server' \; o+ h6 K& q- e( Z4 n. O' f
2008中模拟了Oracle CONNECT BY语句。但是如何在LINQ中做到这一点?6 k5 U# ~% Q2 _& b! ^
这是我正在使用虚拟数据库的示例:
" I) J) j) |% h4 t; pCREATE TABLE Employee(
6 L8 l9 L+ O3 z+ @/ }2 ~& j EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
4 ]- x! f V9 X- w) k Department INT NOT NULL,
3 @- x. @% \, p" T- J7 z% m* ] EmployeeName VARCHAR(40) NOT NULL,
/ n B; I1 Z- F% c4 L$ H) I8 A PeckingOrder INT NOT NULL,; I; g& t- t2 y
HigherDepartment INT NULL)4 K' n* L8 m( y' t) \3 {: q
INSERT INTO Employee (Department,EmployeeName,PeckingOrder,HigherDepartment)$ V# K8 l2 ?" H0 x4 k
VALUES (1,'Bart',1,NULL),(2,'Homer',1,1),(2,'Marge',2,NULL),
1 i2 R3 C* ?8 F7 K% X' ` (3,'Lisa',1,2),(3,'Maggie',2,2),(3,'Santas Helper',3,1)
8 w8 ~3 m+ R9 z# w* ?/ ?EmployeeID Department EmployeeName PeckingOrder HigherDepartment( f( x' C" f- v, J% A4 r' q
1 1 Bart 1 NULL* Y* i5 R; E* ~$ _
2 2 Homer 1 1 % e$ c$ I' ~8 ]2 b0 U# ~+ Y6 G
3 2 Marge 2 NULL
7 s4 S5 l3 t$ L7 T$ j! u* i4 H 4 3 Lisa 1 20 ?) c5 T- L3 M* n( z% [" Q
5 3 Maggie 2 2
9 i% E" i- ]0 m5 d8 Q 6 3 Santas Helper 3 1
. ?+ a6 h d% ?7 p8 x这是用于返回heirachy的SQL: U& G3 _0 r/ L/ {/ H% V; o' u
WITH n(level, PeckingOrder, Department, EmployeeName, HigherDepartment) AS : D/ d) c8 I* @# H1 E3 ?7 N
(SELECT 1, PeckingOrder, Department, EmployeeName, HigherDepartment* z* I5 L& I9 ?8 `- W
FROM Test.dbo.Employee
9 l# f: p( n' R* @ WHERE Department = 3$ o# U( _5 z ?4 H3 @
UNION ALL. B" j) g, D5 a: P) j5 o
SELECT n.level + 1, nplus1.PeckingOrder, nplus1.Department, nplus1.EmployeeName, nplus1.HigherDepartment ' J& t: ^9 g% ]3 y' r) u# L
FROM Test.dbo.Employee as nplus1" P6 e; ^' @' n! s% h
JOIN n ON n.HigherDepartment = nplus1.Department)
1 v' S9 S' _: Y! NSELECT MAX(level) AS level, PeckingOrder, Department, EmployeeName, HigherDepartment
6 o" r9 o1 ^; FFROM n
7 s3 H B; e, \; z* G. ^: KGROUP BY PeckingOrder, Department, EmployeeName, HigherDepartment
" e, d- d& D/ q0 r `ORDER BY MAX(level) DESC, PeckingOrder ASC
& Q t. V. S' f9 f/ Mlevel PeckingOrder Department EmployeeName HigherDepartment6 n7 i% q% _/ k; h8 J, q) B
3 1 1 Bart NULL! G& R* N$ n" ~1 ~2 Y
2 1 2 Homer 1
* z5 L/ T) J, F$ s 2 2 2 Marge NULL
- Z3 ^7 ?$ T1 t/ S% m" |( A 1 1 3 Lisa 2
% ^0 e0 K5 S6 K% |$ Y; j5 o) v. Z 1 2 3 Maggie 2
6 r' s/ G9 q+ @: l 1 3 3 Santas Helper 18 }8 t5 K3 z1 V G
8 ?! Z, E" w, X+ h- `8 ]
解决方案:
7 y3 r; K( A4 O4 p
% K7 P% ^: ^5 l: g1 m$ n
9 \0 p7 r7 l# i+ M9 n' R* L* i! R9 H( O
您可以使用ExecuteQuery:
- w# F+ L7 q$ O) X. r2 S! m% bclass YourRow/ o7 q6 b" n: z8 W7 i
{$ ~4 T% e) j5 b3 D) G' Q( Q7 t
public int level {get; set;}
' K5 l3 o# u" Y% P public int PeckingOrder {get; set;}& a+ t: p( M0 k8 I2 @2 w4 X5 c
...
8 ^7 b5 x g0 M' X( [}
: R7 D$ `& [3 A; [1 X; y% A+ P. kusing (var db = new LinqDataContext())) d8 Y0 Q* C1 s! \; P2 j' h
{+ y) e; l' T1 q r" v* H
var list = db.ExecuteQuery() Y. n4 W; A1 d# a7 Z9 N
@"* V/ n1 R/ W' Z
WITH n(level, PeckingOrder, Department, EmployeeName, HigherDepartment) AS : r3 F" ^7 x" G' f
(SELECT 1, PeckingOrder, Department, EmployeeName, HigherDepartment
: @* c5 i2 H( ?: \...
1 t4 n! z" p6 D& S# f: i& j";% C! F/ G9 R% U. F4 y4 Z7 f
}
, A" g5 R5 V/ ? k6 z% e& x( M也许更好的做法是,创建一个包含查询的视图,然后使用LINQ从该视图读取。 |
|