首页 新闻 搜索 专区 学院

SQL转LINQ写法

0
悬赏园豆:80 [待解决问题]

select * from documents where document_type=4 and id in (select document_id from team_documents where team_id in (select id from teams where id in (select groupid from groups where employess_id=1 ) ))  

 

请教高手,此语句用LINQ 怎么写?  谢谢!  很急! 在线等待

飞比寻常的主页 飞比寻常 | 初学一级 | 园豆:120
提问于:2011-01-24 11:52
< >
分享
所有回答(2)
0

var groupids=dB.Groups.Where(p=>p.Employess_Id==1).Select(p=>p.GroupId);

var teamIds=dB.Teams.Where(p=>groupIds.Contains(p.Id)).Select(p=>p.Id);

var result=db.Documents.Where(p=>p.Document_Type==4&&teamIds.Contains(p.Id));

---------------------------

你这样嵌套了两层,sql的效率很差的,用join好些

SELECT T1.* 

FROM Documents T1

INNER JOIN Team_documents  T2

ON T1.Id=T2.Team_Id

INNER JOIN Groups T3

ON T2.Team_Id =T3.Groupid

WHERE

T1.Document_type=4 AND T3.Employess_id=1

这样sql用linq里面的join 一样,就不写了:)

LittlePeng | 园豆:3395 (老鸟四级) | 2011-01-24 12:59
0

//假设DataContext为TestDbDataContext
TestDbDataContext context = new TestDbDataContext();
var query
= from document in context.documents
where (document.document_type == 4
&& (from team_dcoument in context.team_documents
where (
from team
in context.teams
where (
from gp
in context.groups
where gp.employess_id == 1
select gp.groupid
).Contains(team.id)
select team.id
).Contains(team_dcoument.team_id)
select team_dcoument.document_id
).Contains(document.id))
select document;

 

xfrog | 园豆:240 (菜鸟二级) | 2011-01-26 09:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册