Question

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; }
    }
Was it helpful?

Solution

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

OTHER TIPS

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.

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();
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top