首页 新闻 会员 周边

Linq(急!)

0
[已解决问题] 解决于 2008-11-08 12:24

原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 ?

 

chenming的主页 chenming | 初学一级 | 园豆:0
提问于:2008-11-07 22:00
< >
分享
最佳答案
0

你可以这样

(
  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()

RicoRui | 老鸟四级 |园豆:3663 | 2008-11-08 12:22
其他回答(1)
0

问题应该处在sno==s.SNO上,由于你的sno是from出来的,所以是IQueryable类型,对于IQueable类型我记得有个扩展方法叫.Unique还是叫.Single,反正就那意思,具体是什么我好久没玩linq了,也忘了,你让let sno = Scores.Single(ssss => ssss.Degree == MaxDegree).CNO.ToString()的话,应该就可以选出来了。

具体思路就是这样吧,由于我很久没玩linq,手上也没有你的实体类定义,所以只能给出这样的解答了。呵呵

JimLiu | 园豆:300 (菜鸟二级) | 2008-11-07 23:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册