LINQ 查询结果和字符串方法
首先,让我为代码墙道歉。基本上,我对 Sharepoint 列表有两个两个查询。如果我注释几行,该代码似乎效果很好。这是代码:
全局:
private string mUserName = "";
// Entity classes for the Sharepoint Lists
private SeatingChartContext _dc;
private EntityList<Seating_chartItem> _seatCharts;
private EntityList<UsersItem> _users;
private EntityList<Excluded_usersItem> _exusers;
private EntityList<RoomsItem> _rooms;
private EntityList<LogsItem> _logs;`
页面加载:
// Get the Lists from Sharepoint
_dc = new SeatingChartContext(SPContext.Current.Web.Url);
_seatCharts = _dc.GetList<Seating_chartItem>("seating_chart");
_users = _dc.GetList<UsersItem>("users");
_exusers = _dc.GetList<Excluded_usersItem>("excluded_users");
_rooms = _dc.GetList<RoomsItem>("rooms");
_logs = _dc.GetList<LogsItem>("logs");`
主要代码:
try
{
// - - - - - L O A D T H E * P E O P L E * - - - - -
// Create List objects
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();
var xusersList = (from xuser in _exusers select xuser.User_id).ToList();
usersList = usersList.Where(user => !xusersList.Contains(user.User_id)).ToList();
// Query and use anonymous object for values
var results = from seat in seatList
join user in usersList on
seat.User_id equals 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
};
results = results.Take(5);
foreach (var r in results)
{
ImageButton img = new ImageButton();
img.ID = "seat-" + r.sid;
img.ImageUrl = "http://cxsmoss/rooms/" + r.icon;
img.Style.Add(HtmlTextWriterStyle.Position, "absolute");
img.Style.Add(HtmlTextWriterStyle.Top, r.topCoord + "px");
img.Style.Add(HtmlTextWriterStyle.Left, r.leftCoord + "px");
if (r.name == "")
img.ToolTip = "no name!";
else
img.ToolTip = r.name;
if (r.phone != "")
{
string phn = r.phone;
if (phn.StartsWith("971")) // Comment this line
{
string extension = phn.Substring(phn.Length - 4, 4); //Comment this line
img.ToolTip += Environment.NewLine + "x" + extension;
}
else
img.ToolTip += Environment.NewLine + "x" + phn;
}
if (r.mobile != "")
img.ToolTip += Environment.NewLine + "mobile: " + r.mobile;
img.ToolTip += Environment.NewLine + "room/cubicle: " + r.content.ToLower().Replace("seat ", "");
img.PostBackUrl = ""; // "Default.aspx?name=" + row["name"].ToString();
img.OnClientClick = "UpdateEmployeeInfo('" + r.name.ToString() + "', '" + img.ID + "');return false;";
// For debugging size
img.ToolTip += Environment.NewLine + "Results size " + results.Count();
floorPanel.Controls.Add(img);
}
// - - - - - L O A D T H E * R O O M S * - - - - -
List<Seating_chartItem> seatListRooms = (from seatRoom in _seatCharts where seatRoom.Room == 1 where seatRoom.Floor == floor select seatRoom).ToList();
List<RoomsItem> roomsList = (from room in _rooms select room).ToList();
// Query and use anonymous object for values
var res = from seatRoom in seatListRooms
join room in roomsList on
seatRoom.Seat_id equals room.Room_id
select new
{
rid = room.Room_id,
name = room.Name,
icon = seatRoom.Icon,
topCoord = seatRoom.Top_coord,
leftCoord = seatRoom.Left_coord,
phone = room.Phone,
content = seatRoom.Content
};
foreach (var s in res)
{
ImageButton img = new ImageButton();
img.ID = "room-" + s.rid;
//img.ID = row["icon"].ToString();
img.ImageUrl = "http://cxsmoss/rooms/" + s.icon;
img.Style.Add(HtmlTextWriterStyle.Position, "absolute");
img.Style.Add(HtmlTextWriterStyle.Top, s.topCoord + "px");
img.Style.Add(HtmlTextWriterStyle.Left, s.leftCoord + "px");
img.ToolTip = s.name;
if (s.phone != "")
img.ToolTip += Environment.NewLine + "x" + s.phone;
img.ToolTip += Environment.NewLine + "room " + s.content;
img.OnClientClick = "UpdateRoomInfo('" + s.name + "', '" + img.ID + "');return false;";
img.ToolTip += Environment.NewLine + "Res size " + results.Count();
floorPanel.Controls.Add(img);
}
}
如果我注释掉:
if (phn.StartsWith("971"))
并且
string extension = phn.Substring(phn.Length - 4, 4);
一切运行正常。如果我把它们留在里面,它只会完成第一个 foreach。我尝试重命名第二个查询中的所有变量,显式将 r.phone 转换为字符串并将 ToArray 放在结果查询的末尾。这些都没有帮助。
有趣的是,如果我使用 results = results.Take(5);
来浏览一些房间,我就会得到房间。检查结果计数和 res 计数(第二个查询的结果),它们都是相同的 - 5.
如何重置所有内容以便再次查询我的列表?
First of all let me apologize for the wall of code. Basically, I have two two queries against Sharepoint lists. The code seems to work great if I comment a couple of lines. Here is the code:
Global:
private string mUserName = "";
// Entity classes for the Sharepoint Lists
private SeatingChartContext _dc;
private EntityList<Seating_chartItem> _seatCharts;
private EntityList<UsersItem> _users;
private EntityList<Excluded_usersItem> _exusers;
private EntityList<RoomsItem> _rooms;
private EntityList<LogsItem> _logs;`
Page Load:
// Get the Lists from Sharepoint
_dc = new SeatingChartContext(SPContext.Current.Web.Url);
_seatCharts = _dc.GetList<Seating_chartItem>("seating_chart");
_users = _dc.GetList<UsersItem>("users");
_exusers = _dc.GetList<Excluded_usersItem>("excluded_users");
_rooms = _dc.GetList<RoomsItem>("rooms");
_logs = _dc.GetList<LogsItem>("logs");`
Main code:
try
{
// - - - - - L O A D T H E * P E O P L E * - - - - -
// Create List objects
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();
var xusersList = (from xuser in _exusers select xuser.User_id).ToList();
usersList = usersList.Where(user => !xusersList.Contains(user.User_id)).ToList();
// Query and use anonymous object for values
var results = from seat in seatList
join user in usersList on
seat.User_id equals 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
};
results = results.Take(5);
foreach (var r in results)
{
ImageButton img = new ImageButton();
img.ID = "seat-" + r.sid;
img.ImageUrl = "http://cxsmoss/rooms/" + r.icon;
img.Style.Add(HtmlTextWriterStyle.Position, "absolute");
img.Style.Add(HtmlTextWriterStyle.Top, r.topCoord + "px");
img.Style.Add(HtmlTextWriterStyle.Left, r.leftCoord + "px");
if (r.name == "")
img.ToolTip = "no name!";
else
img.ToolTip = r.name;
if (r.phone != "")
{
string phn = r.phone;
if (phn.StartsWith("971")) // Comment this line
{
string extension = phn.Substring(phn.Length - 4, 4); //Comment this line
img.ToolTip += Environment.NewLine + "x" + extension;
}
else
img.ToolTip += Environment.NewLine + "x" + phn;
}
if (r.mobile != "")
img.ToolTip += Environment.NewLine + "mobile: " + r.mobile;
img.ToolTip += Environment.NewLine + "room/cubicle: " + r.content.ToLower().Replace("seat ", "");
img.PostBackUrl = ""; // "Default.aspx?name=" + row["name"].ToString();
img.OnClientClick = "UpdateEmployeeInfo('" + r.name.ToString() + "', '" + img.ID + "');return false;";
// For debugging size
img.ToolTip += Environment.NewLine + "Results size " + results.Count();
floorPanel.Controls.Add(img);
}
// - - - - - L O A D T H E * R O O M S * - - - - -
List<Seating_chartItem> seatListRooms = (from seatRoom in _seatCharts where seatRoom.Room == 1 where seatRoom.Floor == floor select seatRoom).ToList();
List<RoomsItem> roomsList = (from room in _rooms select room).ToList();
// Query and use anonymous object for values
var res = from seatRoom in seatListRooms
join room in roomsList on
seatRoom.Seat_id equals room.Room_id
select new
{
rid = room.Room_id,
name = room.Name,
icon = seatRoom.Icon,
topCoord = seatRoom.Top_coord,
leftCoord = seatRoom.Left_coord,
phone = room.Phone,
content = seatRoom.Content
};
foreach (var s in res)
{
ImageButton img = new ImageButton();
img.ID = "room-" + s.rid;
//img.ID = row["icon"].ToString();
img.ImageUrl = "http://cxsmoss/rooms/" + s.icon;
img.Style.Add(HtmlTextWriterStyle.Position, "absolute");
img.Style.Add(HtmlTextWriterStyle.Top, s.topCoord + "px");
img.Style.Add(HtmlTextWriterStyle.Left, s.leftCoord + "px");
img.ToolTip = s.name;
if (s.phone != "")
img.ToolTip += Environment.NewLine + "x" + s.phone;
img.ToolTip += Environment.NewLine + "room " + s.content;
img.OnClientClick = "UpdateRoomInfo('" + s.name + "', '" + img.ID + "');return false;";
img.ToolTip += Environment.NewLine + "Res size " + results.Count();
floorPanel.Controls.Add(img);
}
}
If I comment out:
if (phn.StartsWith("971"))
and
string extension = phn.Substring(phn.Length - 4, 4);
everything runs fine. If I leave them in it only completes the first foreach. I have tried renaming all of the variables in the second query, explicitly casting r.phone to string and placing ToArray on the end of the results query. None of these helped.
Interestingly, if I use results = results.Take(5);
to just walk through a few I get the rooms. Checking for the count of results and the count of res (the results of the second query) they are both the same - 5.
How can I reset everything so I can query my lists again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
phn 有可能为空吗?
尝试用 string.IsNullOrEmpty(r.phone) 替换 r.phone != "" 和/或在调试器中打开第一次机会异常。 (调试->异常->公共语言运行时异常[检查])
Is it possible that phn is null?
Try replacing r.phone != "" with string.IsNullOrEmpty(r.phone) and/or turn on first chance exceptions in your debugger. (Debug->Exceptions-> Common Language Runtime Exceptions [check])