This method, FirstOrDefault
, will return null
, if an object not been found. Hence, if you try to read the value of id
, then an exception will be thrown.
One way to avoid this is the following:
// I suppose that the id you want to read is an int.
// If it isn't, please change the code correspondingly.
int id;
// Try to get the record.
var record = db.table.Where(a => a.item == passed_item)
.FirstOrDefault();
// If you find the record you are looking for, then read it's id.
if(record != null)
{
id = record.id;
}
Update
Another option it would be to follow that DavidG
suggested in his comment:
var record = db.Table.FirstOrDefault(a => a.item == passed_item);
and the next step is the same.