原sql:
select distinct s.Sno,c.Cno from student as s,course as c ,score as sc
where s.sno=(select sno from score where degree = (select max(degree) from score))
and c.cno = (select cno from score where degree = (select max(degree) from score))
linq to Sql:
(
from s in Students
from c in Courses
from sc in Scores
let maxDegree = (from sss in Scores
select sss.DEGREE
).Max()
let sno = from ss in Scores
where ss.DEGREE == maxDegree
select ss.SNO.ToString()
let cno = from ssss in Scores
where ssss.DEGREE == maxDegree
select ssss.CNO.ToString()
where sno == s.SNO
select new {
s.SNO,
c.CNO
}
).Distinct()
运行时:
报错:运算符“==”无法应用于“System.Linq.IQueryable<string>”和“string”类型的操作数
如何调整一下linq to sql ?
你可以这样
(
from s in Students
from c in Courses
from sc in Scores
let maxDegree = (from sss in Scores
select sss.DEGREE
).Max()
let sno = (from ss in Scores
where ss.DEGREE == maxDegree
select ss.SNO).Single().ToString()
let cno = (from ssss in Scores
where ssss.DEGREE == maxDegree
select ssss.CNO).Single().ToString()
where s.SNO == sno && c.CNO == cno
select new {
s.SNO,
c.CNO
}
).Distinct()
问题应该处在sno==s.SNO上,由于你的sno是from出来的,所以是IQueryable类型,对于IQueable类型我记得有个扩展方法叫.Unique还是叫.Single,反正就那意思,具体是什么我好久没玩linq了,也忘了,你让let sno = Scores.Single(ssss => ssss.Degree == MaxDegree).CNO.ToString()的话,应该就可以选出来了。
具体思路就是这样吧,由于我很久没玩linq,手上也没有你的实体类定义,所以只能给出这样的解答了。呵呵