优化LINQ to Sharepoint

时间:2011-05-03 22:12:36

标签: linq sharepoint-2010 linq-to-sharepoint

我在Sharepoint 2010上有三个列表,我有工作代码可以获取列表并将它们联系起来。我的问题是加载我的页面大约需要15秒。我是LINQ to Sharepoint和LINQ的初级新手。我的问题是:有没有办法让这段代码运行得更快?

                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
                                         };

至少可以说,此代码所用的时间令人沮丧。

感谢。

1 个答案:

答案 0 :(得分: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查询?

相关问题