Insafian Insafian - 3 months ago 10
C# Question

Check null value in a list using linq

I have a list

List<OfferComparison> Comparison
. I want to
check if all the items have
Value == null
in an
if
condition.

How can I do it with linq?

public class OfferComparison : BaseModel
{
public string Name { get; set; }
public string Value { get; set; }
public bool Valid { get; set; }
}

Answer

Using linq method of All:

var result = Comparison.All(item => item.Value == null)

Basically what it does is to iterate all items of a collection and check a predicate for each of them. If one does not match - result is false

Comments