Iterating over a list of objects, checking if their properties are defined

83 Views Asked by At

I have a list of objects. Is there a way to iterate over the list and check whether any of their properties is not defined?

I have tried something like

list.FindAll(p => p.property == null);

but as a desired property is double I am getting a warning, that the expression will always evaluate to false.

3

There are 3 best solutions below

0
On

Because double can't be null, you can use double?.

Then, continue your logic, it's null means it's not defined

0
On

If the property can't be 0. Basing this response on Yojin response, the easiest way would be to check for 0 instead of null.

list.FindAll(p => p.property == 0);

Of course if at any point the property could be equal to 0 then this should be avoided. There is nullable type of course. The declaration of your property would be something like this :

public double? MyProperty { get; set; }

If nothing is attributed to the property it is null by default.

0
On

Value types can never be null so in the instance of saying

double x;
Console.WriteLine(x.ToString());

You're going to see the '0' get written out.

If you're interested you should check out https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types