0

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

至少可以说,这段代码花费的时间令人沮丧。

谢谢。

4

1 回答 1

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 条件。

还有这些似乎不需要条件的地方:

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

因为你已经过滤了你的seatList这种方式。

话虽如此,您应该记录一些性能数据以查看实际花费最多的时间 - 它是获取初始列表还是您的实际连接/linq 查询?

于 2011-05-03T22:55:42.523 回答