Linq to sql 使用联接和并集进行计数查询

Linq to sql 使用联接和并集进行计数查询,linq-to-sql,Linq To Sql,您好,我想获取此linq查询的计数。我使用实体框架和存储库模式。 可以通过queryUserWalls.ToList().Count()获得结果 我认为这是低效的。 有人能帮忙吗 var queryUserWalls = (from participation in _eventParticipationRepository.GetAll() join eve in _eventRepository.GetAll() on participation.

您好,我想获取此linq查询的计数。我使用实体框架和存储库模式。 可以通过queryUserWalls.ToList().Count()获得结果 我认为这是低效的。 有人能帮忙吗

var queryUserWalls = (from participation in _eventParticipationRepository.GetAll()
                      join eve in _eventRepository.GetAll() on participation.EventId equals eve.Id
                      join userWall in _userWallRepository.GetAll() on participation.EventId equals userWall.EventId
                      where participation.UserId == userId
                      select userWall.Id)

               .Union(from userWall in _userWallRepository.GetAll()
                      select userWall.Id);

省略
ToList
,因为它强制执行查询。您想使用
Queryable.Count
,而不是
Enumerable.Count
。然后,它将在服务器上执行。

为什么不能直接执行
.Count()
?(没有
.ToList()
)为什么不能调用
queryUserWalls.Count()