2011-05-03 72 views
0

我在Sharepoint 2010上有三個列表,我有工作代碼來獲取列表並將它們關聯起來。我的問題是大約需要15秒來加載我的網頁。一般來說,我是LINQ to Sharepoint和LINQ的排名初學者。我的問題是:有沒有辦法讓代碼運行得更快?優化LINQ to Sharepoint

   SeatingChartContext dc = new SeatingChartContext(SPContext.Current.Web.Url); 
       EntityList<Seating_chartItem> seatCharts = dc.GetList<Seating_chartItem>("seating_chart"); 
       EntityList<UsersItem> users = dc.GetList<UsersItem>("users"); 
       EntityList<Excluded_usersItem> exusers = dc.GetList<Excluded_usersItem>("excluded_users"); 
       // EntityList<LogsItem> logs = dc.GetList<LogsItem>("logs"); 

       List<Seating_chartItem> seatList = (from seat in seatCharts where seat.Room == 0 where seat.Floor == floor select seat).ToList(); 
       List <UsersItem> usersList = (from user in users select user).ToList(); 
       List <Excluded_usersItem> xusersList = (from xuser in exusers select xuser).ToList(); 

       var results = from seat in seatList 
           join user in usersList on 
           seat.User_id equals user.User_id 
           where seat.Room == 0 
           where seat.Floor == floor 
           where !(from xuser in xusersList select xuser.User_id).Contains(user.User_id) 
           select new 
             { 
              sid = seat.Seat_id, 
              icon = seat.Icon, 
              topCoord = seat.Top_coord, 
              leftCoord = seat.Left_coord, 
              name = user.Name, 
              phone = user.Phone, 
              mobile = user.Mobile, 
              content = seat.Content 
             }; 

這段代碼需要的時間令人沮喪,至少可以說。

謝謝。

+0

@BrokenGlass:謝謝,那完全是訣竅。再看一遍我的代碼,很明顯我寫了兩個大章節。再次感謝。 – Corey 2011-05-04 20:01:38

回答

1

一個立竿見影的事情:您要重新查詢中xusersList每次您的加盟:

where !(from xuser in xusersList select xuser.User_id).Contains(user.User_id) 

而不只是第一隻提取用戶ID(因爲這是你唯一需要的東西)

var xusersList = (from xuser in exusers select xuser.User_id).ToList(); 

然後直接使用它:

where !xusersList.Contains(user.User_id) 

更妙的是 - 你的查詢之前確定有效的用戶:

usersList = usersList.Where(user => !xusersList.Contains(user.User_id)) 
        .ToList(); 

現在,你可以完全刪除這個地方從您的查詢條件。

而且這些地方的條件似乎是不必要的:

where seat.Room == 0 
where seat.Floor == floor 

既然你已經過濾出的seatList這樣了。

話雖如此,你應該記錄一些性能數據,看看實際需要多少時間 - 是獲取初始列表還是實際的join/linq查詢?