把多表联查的sql语句转换成linq语句

问题描述

把多表联查的sql语句转换成linq语句

SELECT * FROM [group_product]
where proid in(select proid from [group_browsehistory]
where memid in(select memid from [group_browsehistory] where proid=12))

在线等,这个sql语句怎么转换成linq???

解决方案

 from x in group_product
where group_browsehistory.Select(y => y.proid).Where(z => group_browsehistory.Where(v => v.proif == 12).Select(w => w.memid).Contains(z)).Contains(x,proid)

解决方案二:

x,proid
->
x.proid

解决方案三:

最好加上top 和 order by 如下:
SELECT top(2)* FROM [group_product]
where proid in(select proid from [group_browsehistory]
where memid in(select memid from [group_browsehistory] where proid=12))
order by apptime desc

我是想获取浏览过proid=12的商品的人浏览过的其他商品。

解决方案四:

谢谢caozhy

可是有报错

怎么弄?

解决方案五:

多表联合查询SQL语句
sql语句一例:多表联合查询
SQL语句多表查询

解决方案六:

@caozhy 谢谢啦! 现在不报错了,调试成功,即来结贴!

解决方案七:

生成的语句错的........ 以下是生成的sql语句,把各查询字段改成了* 以便查看:
SELECT *
FROM [dbo].[group_product] AS [t0]
WHERE EXISTS(
SELECT NULL AS [EMPTY]
FROM [dbo].[group_browsehistory] AS [t1]
WHERE ([t1].[proid] = [t0].[proid]) AND (EXISTS(
SELECT NULL AS [EMPTY]
FROM [dbo].[group_browsehistory] AS [t2]
WHERE ([t2].[memid] = [t1].[proid]) AND ([t2].[proid] = 12)
))
)
查询出来是空的......
怎么办?

解决方案八:

谢谢大家~已经解决啦,分成了2个语句:
//用户
var users = (from o in db.Group_browsehistory
where o.Proid == proid
group o by o.Memid into br
select new
{
Key = br.Key,
Count = br.Count()
}).ToList();

        List<int> ids = users.Select(o => o.Key).ToList();
        var q = (
                 from b in db.Group_browsehistory
                 where ids.Contains(b.Memid)
                 group b by b.Proid into pr
                 join p in db.Group_product on pr.Key equals p.Proid
                 select new GProduct
                 {
                     Proid = p.Proid,
                     Name = p.Name,
                     Mainpic = p.Mainpic,
                     Userprice = p.Userprice,
                     Unuserprice = p.Unuserprice,
                     Shopprice = p.Shopprice
                 }).Distinct();

        var list = q.Take(count).ToList();

解决方案九:

额..............这个该怎么结贴??

时间: 2016-03-11
Tags: sql, linq

把多表联查的sql语句转换成linq语句的相关文章

关于SQL语句转换成linq语句

问题描述 关于SQL语句转换成linq语句 SELECT * FROM dbo.[USER] u, (SELECT ua.User_Attention_Object_Id FROM dbo.User_Attention ua,dbo.[USER] u WHERE u.User_Id=ua.User_Attention_User_Id AND ua.User_Attention_User_Id=3) uaid WHERE u.User_Id=uaid.User_Attention_Object_I

hibernate-原始sql语句转换成jpql语句

问题描述 原始sql语句转换成jpql语句 如题,现在有一个sql语句: SELECT c.* FROM fs_gulib a, fs_star c,(SELECT t2.id,COUNT(*) FROM fs_gulib_member t1 LEFT JOIN fs_gulib t2 ON t1.gulib = t2.id GROUP BY t1.gulib ORDER BY COUNT(*) DESC) n WHERE a.id=c.gulib_id AND a.id=n.id; 如何转成j

如何将下面的sql语句转换成Linq形式

问题描述 想把下面的sql语句转换成LINQ形式具体的sql语句如下:select*from(select字段,row_number()over(排序方式)asrownumfrom表名where条件)asbwhereb.rownum>起始值andb.rownum<=结束值orderby排序方式其中所有的汉字均是通过变量进行传值的不知道这个怎么转换成LINQ形式大侠们给帮帮忙写写吧! 解决方案 解决方案二:varq=(fromaindb.table1where...orderby字段descen

新手 SQL语句转换成LINQ [谢谢啊]

问题描述 SQLSERVER2008简单小统计:selecta=sum(casewhenstates=1then1else0end),b=sum(casewhenstates=2then1else0end),c=count(*)frompeople 怎么转换成LINQ啊不怎么会LINQ帮帮忙谢谢啊. 解决方案 解决方案二:inta=people.Sum((p)=>p.states=1);intb=people.Sum((p)=>p.states=2);intc=people.Count();

sql语句转换成hql求大神解决

问题描述 sql语句转换成hql求大神解决 具体sql语句如下,变成hql语句,请高手知道一下.select * from plan_run r where r.id in(select t.plan_run_waishi_user_id from PLAN_RUN_OA_USER t where t.oa_user_id= ?) 解决方案 from 实体类1 p where p.id in ( select i.id from 实体类2 i where i.useId = ? )注意hql里面

mysql的查询sql语句转换成oracle的sql

问题描述 mysql的查询sql语句转换成oracle的sql SELECT CREATE_DATE SUM(ISBIND) AS ISBIND SUM(UNBIND) AS UNBIND SUM(SUBSCRIBE) AS SUBSCRIBE SUM(UNSUBSCRIBE) AS UNSUBSCRIBE FROM ( SELECT N.CREATE_DATE MAX( CASE N.EVENT WHEN 'subscribe' THEN COUNT ELSE 0 END ) SUBSCRIB

系统-sql server转换成access数据库

问题描述 sql server转换成access数据库 如果我有一个系统是java语言和sql server数据库,想转换成access数据库,怎么办 解决方案 把sqlserver数据库转成sql,在access里执行是一种办法,也可以参考这个 尝试将sqlserver 中数据导入到access中http://77857.blog.51cto.com/67857/161106/ 解决方案二: 1.换数据库挺麻烦的,先要转数据库结构,然后还要导数据. 2.原先的java连sql server要改

select-asp 文件转换php实现,主要是SQLSERVER里的存储过程转换成mysql语句写不出来!

问题描述 asp 文件转换php实现,主要是SQLSERVER里的存储过程转换成mysql语句写不出来! =================存储过程======================= ALTER PROCEDURE [dbo].[CreatUser] -- Add the parameters for the stored procedure here @Username varchar(50), @userPassword varchar(50), @QQID varchar(11

SQL语句转成LINQ:select *, (case tab1 when 1 then &amp;amp;#x27;y&amp;amp;#x27; when 2 then &amp;amp;#x27;N&amp;amp;#x27; end ) as test

问题描述 SQL语句转成LINQ:select*,(casetab1when1then'y'when2then'N'end)astestfromTaleftjoinTbONTa.pk_id=Tb.Fk_idwhereTalike'%key%' 解决方案 解决方案二:varq=fromtaindb.Tajointbindb.Tbonta.pk_idequalstb.Fk_idintotempfromttintemp.DefaultIfEmpty()whereta.Ta.Contains("key&