2011-05-03 74 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查询?