Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq到实体计数子查询_C#_Sql Server_Linq To Entities - Fatal编程技术网

C# Linq到实体计数子查询

C# Linq到实体计数子查询,c#,sql-server,linq-to-entities,C#,Sql Server,Linq To Entities,好的,我有很多地方用户可以评分。在获取位置时,我还想知道有多少朋友对该位置进行了评分。用户通过发送请求成为朋友 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.*, ( s

好的,我有很多地方用户可以评分。在获取位置时,我还想知道有多少朋友对该位置进行了评分。用户通过发送请求成为朋友

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
我真的很难在LINQtoEntities中提出一个相互查询(我是一个初学者),并提出了这个

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

是我的朋友系统模型让这个查询有点笨重吗?任何人都有任何认可的技巧,可以让它更高效、更容易阅读吗?

一个问题-为什么每次查询都要重新计算好友数?您可以找出朋友,并在用户对象中对他们进行引用。这要简单得多

var friendPlaces = users.Friends.Reviews.Where(r => r.PlaceId == placeID);
甚至更好

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

让应用程序的每个部分都了解基本的数据库结构。如果您必须在两个月内更改模式,会发生什么情况?

您能再解释一下吗?和lambda在一起很艰难。我无法访问用户。像您一样访问朋友。您需要使用Friends属性编写Users类。它不存在。但是用户类是从我的sql server生成的?您可以扩展该类,或者创建自己的用户类,与sql server生成的类对话。让业务逻辑完全依赖于SQL server结构并不是一种好的做法,这是一种典型的ORM反模式。