把sql语句转为Linq

apollo33022 2011-08-04 04:32:27
select * from Core_Module where IsDelete=0 and ModuleType<>3
and ParentID in(
select ID from Core_Module where ParentID = @parentid union select @parentid as ID )
order by sequence

帮忙把这个sql语句转成Linq的形式。其中 @parentid 是传进来的参数
...全文
147 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
apollo33022 2011-08-05
  • 打赏
  • 举报
回复
解决了,只要再加个条件就行。如下:
var query=from c in db.Core_Module
let temp=db.Core_Module.Where(d=>d.ParentID== @parentid).Select(d=>d.ID)
where (c.ParentID==@parentid || temp.Contains(c.ParentID)) && c.IsDelete==0 && c.ModuleType != 3
orderby c.sequence
select c;
apollo33022 2011-08-05
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 q107770540 的回复:]
看来是union select 的问题
先试试 这样行不行:

var query=from c in db.Core_Module
let temp=db.Core_Module.Where(d=>d.ParentID== @parentid).Select(d=>d.ID)
where temp.Contains(c.ParentID) && c.IsDele……
[/Quote]
这样不会报错了。但好像少了个条件
q107770540 2011-08-05
  • 打赏
  • 举报
回复
看来是union select 的问题
先试试 这样行不行:

var query=from c in db.Core_Module
let temp=db.Core_Module.Where(d=>d.ParentID== @parentid).Select(d=>d.ID)
where temp.Contains(c.ParentID) && c.IsDelete==0 && c.ModuleType != 3
orderby c.sequence
select c;
apollo33022 2011-08-05
  • 打赏
  • 举报
回复
4楼得报错:不能在查询运算符(Contains() 运算符除外)的 LINQ to SQL 实现中使用本地序列。
永动bug制造机 2011-08-05
  • 打赏
  • 举报
回复
又见猫哥。
q107770540 2011-08-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 apollo33022 的回复:]
select * from Core_Module where IsDelete=0 and ModuleType<>3
and ParentID in(
select ID from Core_Module where ParentID = @parentid union select @parentid as ID )
order by sequence

帮忙把这个sql语句转……
[/Quote]
var query=from c in db.Core_Module
let temp=db.Core_Module.Where(d=>d.ParentID== @parentid).Select(d=>d.ID).Union(new int[]{@parentid})
where c.IsDelete==0 && c.ModuleType != 3 && temp.Contains(c.ParentID)
orderby c.sequence
select c;

ustbwuyi 2011-08-04
  • 打赏
  • 举报
回复

from c in db.Core_Modules
where (from t in db.Core_Module where t.ParentID=@parentid).ToList().Contains(c.ParentID) && c.IsDelete==0 && c.ModuleType!=3
select c


暖枫无敌 2011-08-04
  • 打赏
  • 举报
回复
aegisho 2011-08-04
  • 打赏
  • 举报
回复
in可以用contains,
(form ……select ).contains(xx.parentid) 试试看

8,492

社区成员

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

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