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

2660 views c#
0

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.

answered question

A double can never be null, so what are you expecting to happen?

Can you use p => p.Property == default(double)?

You need to create a comparer with a define "null" equivalent value for non nullable types. Example you can decide that double are null when they are equal to double.MinValue. Because even using default(type) it may return a valid value for you.

What "property is not defined" means in your context? If you need an "any" expression you can use the .Any(/* expression */) extension method on IEnumerable

Maybe you want to use double?

1 Answer

10

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://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types

posted this

Have an answer?

JD

Please login first before posting an answer.