请问这个长长的SQL查询语句能转换为LINQ吗?

shems 2011-01-06 11:59:23
下面是原来的SQL语句。前面的可以不用太理会,主要是最后一个left join,连接了一个临时表([故障记录表]和[故障处理表]是一对多的关系,由于一条记录对应多条处理结果,而只需要查出最后处理的一条记录,所以用了max(a.[处理时间]))。

SELECT [故障记录表].[序号]
,[故障记录表].[分类]
,[终端信息表].[品牌]
,[网点信息表].[隶属支行]
,[网点信息表].[网点名] as 网点
,[用户信息表].[用户名] as 登记人
,[故障记录表].[标签号]
,[故障分类]
,[故障描述]
,[登记时间]
,[最后处理时间]
,[故障记录表].[处理标志]
FROM [故障记录表]
left join [网点信息表] on [故障记录表].[网点号] = [网点信息表].[网点号]
left join [用户信息表] on [故障记录表].[登记人] = [用户信息表].[登录ID]
left join [终端信息表] on [故障记录表].[标签号] = [终端信息表].[标签号]
left join
(
select a.[故障编号]
,a.[处理标志]
,max(a.[处理时间]) as [最后处理时间]
from [故障处理表] as a,(SELECT distinct [故障编号],[处理标志] FROM [TML].[dbo].[故障处理表]) as b
where a.[故障编号]=b.[故障编号] and a.[处理标志]=b.[处理标志]
group by a.[故障编号],a.[处理标志]
) as table1 on [故障记录表].[序号] = table1.[故障编号] and [故障记录表].[处理标志] = table1.[处理标志]


下面是我尝试转换成的LINQ:

var temp1 = (from p in this.context.故障处理表
select new { p.故障编号, p.处理标志 }).Distinct();

var temp2 = from p in this.context.故障处理表
join o in temp1
on new { p.故障编号, p.处理标志 } equals new { o.故障编号, o.处理标志 }
select new { p.故障编号, p.处理标志, p.处理时间 };

var temp3 = from p in temp2
group p by new { p.故障编号, p.处理标志 } into g
select new { g.Key.故障编号, g.Key.处理标志, 最后处理时间 = g.Max(p => p.处理时间) };

var result = from p in this.context.故障记录表
join o1 in this.context.网点信息表 on p.网点号 equals o1.网点号 into gj1
join o2 in this.context.用户信息表 on p.登记人 equals o2.登录ID into gj2
join o3 in this.context.终端信息表 on p.标签号 equals o3.标签号 into gj3
join o4 in temp3 on new {p.序号, p.处理标志} equals new {o4.故障编号, o4.处理标志} into gj4
from g1 in gj1.DefaultIfEmpty()
from g2 in gj2.DefaultIfEmpty()
from g3 in gj3.DefaultIfEmpty()
from g4 in gj4.DefaultIfEmpty()
select new
{
p.序号,
p.分类,
p.标签号,
p.故障分类,
p.故障描述,
p.登记时间,
p.处理标志,
g1.隶属支行,
网点 = g1.网点名,
登记人 = g2.用户名
};

前面的temp1到temp3其实就是对上面的SQL语句最后一个left join临时表做了一个查询,结果保存到temp3里。
最后的result其实就是照搬SQL语句,只是最后的left join成了temp3而已。

问题是这样编译不通过,编译器对最后一个join子句报错:“join子句中起重一个表达式的类型不正确。对join的调用中地类型推理失败”。

我试过取消掉temp1到temp3的临时变量,直接把查询写到join里,结果一样。

难道是临时表的查询太复杂了,所以无法自动连接到linq to sql里面?

对于这个查询,有没有可能直接转换为linq to sql?

请各位指教。谢谢!
...全文
117 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
q107770540 2011-01-06
  • 打赏
  • 举报
回复


var result = from p in this.context.故障记录表
let temp1 = (from p in this.context.故障处理表
select new { p.故障编号, p.处理标志 }).Distinct()
let temp2 = from p in this.context.故障处理表
join o in temp1
on new { p.故障编号, p.处理标志 } equals new { o.故障编号, o.处理标志 }
select new { p.故障编号, p.处理标志, p.处理时间 }
let temp3 = from p in temp2
group p by new { p.故障编号, p.处理标志 } into g
select new { g.Key.故障编号, g.Key.处理标志, 最后处理时间 = g.Max(p => p.处理时间) }

join o1 in this.context.网点信息表 on p.网点号 equals o1.网点号 into gj1
from g1 in gj1.DefaultIfEmpty()
join o2 in this.context.用户信息表 on g1.登记人 equals o2.登录ID into gj2
from g2 in gj2.DefaultIfEmpty()
join o3 in this.context.终端信息表 on g2.标签号 equals o3.标签号 into gj3
from g3 in gj3.DefaultIfEmpty()
join o4 in temp3 on new {g3.序号, g3.处理标志} equals new {o4.故障编号, o4.处理标志} into gj4

from g4 in gj4.DefaultIfEmpty()
select new
{
p.序号,
p.分类,
p.标签号,
p.故障分类,
p.故障描述,
p.登记时间,
p.处理标志,
g1.隶属支行,
网点 = g1.网点名,
登记人 = g2.用户名
};
q107770540 2011-01-06
  • 打赏
  • 举报
回复
经验就是这样积累来的
要懂得总结
不要一段时间又忘记了
shems 2011-01-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 q107770540 的回复:]

join o4 in temp3 on new {g3.序号, g3.处理标志} equals new {o4.故障编号, o4.处理标志} into gj4
===
join o4 in temp3 on new {t1=g3.序号, t2=g3.处理标志} equals new {t1=o4.故障编号, t2=o4.处理标志} into gj4

这样就好了
[/Quote]

果然行了,没想到跟假名有关系……

衷心地感谢您!这问题搞了我老半天了,泪流满面……
q107770540 2011-01-06
  • 打赏
  • 举报
回复
join o4 in temp3 on new {g3.序号, g3.处理标志} equals new {o4.故障编号, o4.处理标志} into gj4
===
join o4 in temp3 on new {t1=g3.序号, t2=g3.处理标志} equals new {t1=o4.故障编号, t2=o4.处理标志} into gj4

这样就好了
shems 2011-01-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 q107770540 的回复:]

将temp3 单独定义在查询语句之外 试试看
[/Quote]

嘛,那不是回到我最初的问题了吗?

您再看看顶楼,编译器对最后一个join子句报错:“join子句中其中一个表达式的类型不正确。对join的调用中的类型推理失败”。
q107770540 2011-01-06
  • 打赏
  • 举报
回复
将temp3 单独定义在查询语句之外 试试看

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧