如何将这个sql转换为linq

fyrise 2011-06-17 11:16:45
sql语句

select * from Viewstscore where stu_id in (select stu_id from Viewstscore group by stu_id having count(*)>1)
目的

视图Viewstscore中 字段的值可能有重复,我要查找Viewstscore 中stu_id有 重复的数据,用linq不知如何写

...全文
70 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2011-06-18
  • 打赏
  • 举报
回复
var query1 = from x in Viewstscore group x bu x.stu_id into g where g.Count() > 1 select g;
var query = query1.First();
foreach (var i in query1.Skip(1))
{
query = query.Union(i);
}

query 是你要的。
fyrise 2011-06-18
  • 打赏
  • 举报
回复
如果我想将查出来信息的写到一个表内,而不是按stu_id写入多个表,该怎么写
threenewbee 2011-06-18
  • 打赏
  • 举报
回复
嗯,Count() 要加上括号,疏忽了。
fyrise 2011-06-17
  • 打赏
  • 举报
回复
已解决,谢谢
fyrise 2011-06-17
  • 打赏
  • 举报
回复
var query = from x in Viewstscore group x by x.stu_id into g where g.Count > 1 select g;

楼上的出错提示
g.Count > 1 运算符>无法应用于方法组和int类型的操作数
threenewbee 2011-06-17
  • 打赏
  • 举报
回复
var query = Viewstscore.Where(y => (from x in Viewstscore group x by x.stu_id into g select g.Key).Where(z => z.Count() > 1).Contains(y)).Select(x => x);


其实不用这么麻烦
var query = from x in Viewstscore group x bu x.stu_id into g where g.Count > 1 select g;
foreach (var i in query)
{
g.ToList().ForEach(x => Console.Writeline(x.stu_id));
}
threenewbee 2011-06-17
  • 打赏
  • 举报
回复
var query = Viewstscore.Where(y => (from x in Viewstscore group x by x.stu_id into g select g.Key).Contains(y)).Select(x => x);

8,494

社区成员

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

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