2
I need to remove all null fields, which comes from a list of Phones
, I tried to do it this way, but unfortunately it didn’t work:
model.Phones = model.Phones.Where(x => x.Phone != null && x.Phone != "")
.Distinct()
.ToList();
received the following error:
Nullreferenceexception: Object Reference not set to an instance of an Object.
It may seem like a stupid question, but the Nullreference error occurred using this image data?
– Ronaldo Araújo Alves
Not exactly, this image I made with an example, but also generate the error. Where it generated the error instead of being
{Konbase.Areas.Admin....}
wasnull
– Matheus
In case that phone list is in memory or you bring it straight from the database?
– Guilherme Caixeta
Actually she comes from a
form
, i dynamically Gero fields ofTelefones
withBeginCollection
, so the user can type as many phone fields as he wants, thus leaving inputsnull
if he doesn’t fill in all.– Matheus
The command is correct. The error probably occurs by a
model
or themodel.Phones
null. Try to validate ifnull
before using the command.– Ronaldo Araújo Alves
Your problem then is in sending the form, it should send "only" the phones that have values. But you can delete to be able to remove the values, something like this: "model.Phones.Remove(x => string.Isnullorempty(x.Phone)". With that should solve the problem.
– Guilherme Caixeta