Reputation: 121
Im having a method like this
public List<GSMData> GetGSMList()
{
return meters.Select(x => x.Gsmdata.Last())
.ToList();
}
and i get a "Value cannot be null." exeption. How do i make it so the value can be null?
GSMData object
public class GSMData : Meter
{
public DateTime TimeStamp { get; set; }
public int SignalStrength { get; set; }
public int CellID { get; set; }
public int LocationAC { get; set; }
public int MobileCC { get; set; }
public int MobileNC { get; set; }
public string ModemManufacturer { get; set; }
public string ModemModel { get; set; }
public string ModemFirmware { get; set; }
public string IMEI { get; set; }
public string IMSI { get; set; }
public string ICCID { get; set; }
public string AccessPointName { get; set; }
public int MobileStatus { get; set; }
public int MobileSettings { get; set; }
public string OperatorName { get; set; }
public int GPRSReconnect { get; set; }
public string PAP_Username { get; set; }
public string PAP_Password { get; set; }
public int Uptime { get; set; }
}
Upvotes: 0
Views: 73
Reputation: 4726
Try excluding null items by filtering the list immediately, and before doing the select.
public List GetGSMList() { return meters.Where(x=> x.Gsmdata != null).Select(x => x.Gsmdata.Last()) .ToList(); }
Upvotes: 0
Reputation: 63065
you better check for Gsmdata having items or not
public List<GSMData> GetGSMList()
{
return meters.Where(x=>x.Gsmdata!=null && x.Gsmdata.Any()).Select(x => x.Gsmdata.Last())
.ToList();
}
Upvotes: 3
Reputation: 1676
I'm guessing your Gsmdata
property is null
.
In this case, you could use the Linq .Where()
method, to make this:
public List<GSMData> GetGSMList()
{
return meters.Where(x => x.Gsmdata != null)
.Select(x => x.Gsmdata.Last())
.ToList();
}
But you should clarify what exactly is null for better answers.
Upvotes: 0