0

好的,所以我有很多用户可以评价的地方(打分)。在获取地点时,我还想知道有多少朋友评价了那个地点。用户通过发送请求成为朋友。

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()
                 })

是我的朋友系统模型使这个查询有点笨重吗?任何人都有任何可以完成的批准提示,使其更高效,更易于阅读?

4

1 回答 1

0

一个问题 - 你为什么要为每个查询重新计算朋友?您可以找出朋友,并将对他们的引用放在 User 对象中。做起来要简单得多

var friendPlaces = users.Friends.Reviews.Where(r => r.PlaceId == placeID);

甚至更好

var friendPlace= users.Friends.Select(GetPlacesReviewed).Where(p => p.Id == placeID);

而不是让应用程序的每个部分都知道您的基本数据库结构。当您必须在 2 个月内更改架构时会发生什么?

于 2013-11-08T22:22:46.183 回答