我需要将此SQL查询转换为链接:
"Select * FROM [Register]
where RegisterId IN (SELECT MyId
FROM Friends
WHERE FriendId='" + Session["CurrentProfileId"] + "'
AND Status=1
UNION
SELECT FriendId
FROM Friends
WHERE MyId='" + Session["CurrentProfileId"] + "'
AND Status=1) ";
可能看起来像这样?但这是错误的并且有错误
(from u in db.Register
where RegisterId).Contains
(from f in db.Freinds
where f.MyId == Id && m.Status == 1
select new { m.MyId })
.Union(from m in db.Freinds
where f.FreindId == Id && m.Status == 1
select new { m.CreateDate } ));
答案 0 :(得分:1)
上面的linq有一些问题,这里有几个:
Union
的查询中,您选择CreateDate
,而在顶部则选择MyId
。我假设您打算选择FreindId
。RegisterId
进行比较,该guid
可能是string
/ int
/ Contains
- 但肯定不是你刚才创造的类型。var idsCollection = ((from f in db.Freinds
where f.StatusId == 1 && f.MyId == Id
select f.MyId)
.Union(from m in db.Friends
where m.StatusId == 1 && f.FreindId == Id
select m.FriendId)).ToList();
var result = (from u in db.Register
where idsCollection.Contains(u.RegisterId)
select u).ToList();
方法错误。 Linq语法可以类似于sql,但它不一样。点击此处查看Contains
正确的Linq方式是:
.ToList()
请注意.ToList()
不是必须的,这只是为了便于调试。有关此{{1}}和Linq的详细信息,请参阅MSDN