日期:2014-05-20  浏览次数:21045 次

请问大神,带exists语句用linq to entities怎么写呢?
请问大神,带exists语句用linq to entities怎么写呢?

表结构:

CREATE TABLE [dbo].[a](
[id] [int] NULL,
[name] [nvarchar](50) NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[b](
[id] [int] NULL,
[name] [nvarchar](50) NULL
) ON [PRIMARY]



数据:


insert into a(id,name)

select 1,'张山'
union all
select 2,'李思'
union all 
select 3,'王五'


insert into b(id,name)

select 1,'张山'
union all
select 2,'李思'


查询语句:
select id,name  from a where exists(select top 1 1 from b where b.id=a.id)



这个查询语句用linq to entities应该怎么写呢?



------解决方案--------------------
如果按你的要求是:
a.where(p=>b.any(c=>c.id==p.id)).select(p=>new {p.id,p.name})

不过说实话这种写法很别扭,你应该直接使用join,grouyjoin,left join一类写法

------解决方案--------------------
用Any(条件)或者Where(条件).Count() > 0
------解决方案--------------------

select id,name  from a where exists(select top 1 1 from b where b.id=a.id)


from p in a where b.Any(g=>g.id==p.id) select p