LINQ Risultati della query e metodi di stringa
-
29-10-2019 - |
Domanda
Prima di tutto lasciatemi scusarmi per il muro di codice.Fondamentalmente, ho due due domande sugli elenchi di Sharepoint.Il codice sembra funzionare alla grande se commento un paio di righe.Ecco il codice:
Globale:
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;`
Caricamento pagina:
// 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");`
Codice principale:
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);
}
}
Se commento fuori:
if (phn.StartsWith("971"))
e
string extension = phn.Substring(phn.Length - 4, 4);
tutto funziona correttamente.Se li lascio dentro, completa solo il primo foreach.Ho provato a rinominare tutte le variabili nella seconda query, trasmettendo esplicitamente r.phone alla stringa e posizionando ToArray alla fine della query dei risultati.Nessuno di questi ha aiutato.
È interessante notare che se utilizzo results = results.Take(5);
per esaminarne alcuni, ottengo le stanze.Controllando il conteggio dei risultati e il conteggio delle res (i risultati della seconda query) sono entrambi uguali - 5.
Come posso reimpostare tutto in modo da poter interrogare nuovamente i miei elenchi?
Soluzione
È possibile che phn sia nullo?
Prova a sostituire r.phone!="" con string.IsNullOrEmpty (r.phone) e / o attiva le eccezioni di prima possibilità nel tuo debugger.(Debug-> Eccezioni-> Eccezioni di Common Language Runtime [verifica])