Insafian Insafian - 3 months ago 20
C# Question

LINQ query for list

I have a list

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

Is there a way I can use any
LINQ
query to check this in an
if
condition?

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