2013-08-30 96 views
0

嗨我想獲得這個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); 
+0

爲什麼不能直接執行'.Count()'? (no'.ToList()') – xanatos

+0

爲什麼你不能調用'queryUserWalls.Count()'? – MarcinJuraszek

回答

1

由於它強制執行查詢,因此省略ToList。你想使用Queryable.Count,而不是Enumerable.Count。然後,它將在服務器上執行。