好的,所以我有很多用户可以评价的地方(打分)。在获取地点时,我还想知道有多少朋友评价了那个地点。用户通过发送请求成为朋友。
FRIENDREQID -- ID of user that sent the request.
FRIENDRESPID -- ID of user that got the request
STATUS -- Status of the request. (Accepted / Pending)
这个查询完成了这项工作。
select
p.*,
(
select
count(*)
from
UserRatings ur,
Friends f
where
ur.PlaceId = p.PlaceId
and (ur.UserId = f.FriendReqId and f.FriendRespId = @myUserId )
or (ur.UserId = f.FriendRespId and f.FriendReqId = @myUserId )
and f.Status = 'A'
) as FriendCount
from
Places p
我真的很难在 linq 中对实体进行相互查询(我是初学者)并想出了这个。
var result = (from p in db.Places
select new PlaceDTO
{
PlaceId = p.PlaceId,
FriendRatingCount = (from f in db.Friends
from ur in db.UserRatings
where ur.PlaceId == p.PlaceId &&
(ur.UserId == f.FriendReqId && f.FriendRespId == argUserId) ||
(ur.UserId == f.FriendRespId && f.FriendReqId == argUserId) &&
f.Status == "A"
select f).Count()
})
是我的朋友系统模型使这个查询有点笨重吗?任何人都有任何可以完成的批准提示,使其更高效,更易于阅读?