I have a SharePoint list that I'm reading. In this list there are a number of items that don't have a value assigned to them, throwing an error. The code I'm using is the following:
public class FacilitiesDal : BaseDal
{
public List<FacilitiesEntity> FetchItems(string siteName, string listUrl)
{
try
{
using (var site = new SPSite(siteName))
{
using (var web = site.OpenWeb())
{
PostEvent("Successfully opened: " + web.Url, BaseExceptionEventArgs.ExceptionLevel.Debug);
PostEvent("Attempting to load list: " + listUrl, BaseExceptionEventArgs.ExceptionLevel.Debug);
return (from SPListItem item in web.GetList(listUrl).Items
select LoadItems( item["Assigned To"].ToString() ?? "Unassigned",
item["Site(s)"].ToString(),
item["Job Category"].ToString(),
item["Status"].ToString(),
Convert.ToDateTime(item["Date required?"]),
item.ID.ToString(),
item.ContentType.Name,
item.DisplayName,
item.Name,
"",
item.Url,
item["Created By"].ToString(),
item["Modified By"].ToString(),
Convert.ToDateTime(item["Created"]),
item["Created By"].ToString(),
Convert.ToDateTime(item["Created"]),
item["Created By"].ToString())).ToList();
}
}
}
catch (Exception ex)
{
PostEvent("Error fetching Facility list items", BaseExceptionEventArgs.ExceptionLevel.Error, ex);
throw;
}
}
The following line being the problem:
select LoadItems( item["Assigned To"].ToString() ?? "Unassigned",
If I change that line to not try to read the assigned to field, like the following, it works fine:
select LoadItems( "Unassigned",
All I can deduce from this is that the null collase operator I'm using here to evaluate if the assigned to field is blank or not isn't working as I'm expecting it to, but I can't understand why. How am I meant to be thinking about this?