select sqc_name,sqc_code from [ald_address_New].[dbo].[tb_address_SQC] t1 where
sqc_parentXzcCode is null and sqc_code not in
( select distinct sqc_parentXzcCode from [ald_address_New].[dbo].[tb_address_SQC]
where sqc_parentXzcCode is not null
)
union
select isnull(t2.sqc_name,'')+t1.sqc_name,t1.sqc_code from [ald_address_New].[dbo].[tb_address_SQC] t1
left join [ald_address_New].[dbo].[tb_address_SQC] t2 on t1.sqc_parentXzcCode=t2.sqc_code
where t1.sqc_parentXzcCode is not null
将上面的SQL语句转成LINQ语句 ,感谢
问题解决,未测试性能,代码如下:
1 var q = (from p in aae.tb_address_SQC
2 where p.sqc_xzjd_code == id && p.sqc_parentXzcCode == null && !((from t in aae.tb_address_SQC
3 where
4 t.sqc_xzjd_code == id &&
5 t.sqc_parentXzcCode != null
6 select t.sqc_parentXzcCode).
7 Distinct()).Contains(
8 p.sqc_code)
9 select new {name = p.sqc_name, code = p.sqc_code, parent = p.sqc_xzjd_code}).Union(
10 from p1 in aae.tb_address_SQC
11 from p2 in aae.tb_address_SQC
12 where p1.sqc_xzjd_code == id && p1.sqc_parentXzcCode == p2.sqc_code
13 select
14 new
15 {
16 name = (p2.sqc_name == null ? "" : p2.sqc_name) + p1.sqc_name,
17 code = p1.sqc_code,
18 parent = p1.sqc_xzjd_code
19 }
20 );