优化 LINQ to Sharepoint

发布于 2024-11-04 18:18:50 字数 2119 浏览 1 评论 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
                                         };

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

谢谢。

I have three lists on Sharepoint 2010 and I have working code that gets the lists and relates them. My problem is that it takes around 15 seconds to load my page. I am a rank beginner with LINQ to Sharepoint and LINQ in general. MY question is: Is there a way to make this code run faster?

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

The time this code takes is frustrating, to say the least.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌入人心 2024-11-11 18:18:50

一件直接的事情:您每次在加入中都会重新查询 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

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

One immediate thing: You are re-querying xusersList everytime within your join:

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

Instead just first extract the user ids only (since that is the only thing you need)

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


then use it directly:

  where !xusersList.Contains(user.User_id)

Even better - determine the valid users before your query:

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

Now you can just completely remove this where condition from your query.

Also these where conditions seem to be unneeded:

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

since you have filtered your seatList this way already.

Having said that you should log some performance data to see what actually takes the most time - is it acquiring the inital lists or your actual join/linq query?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文