1

Can we do something similar to List.Contains(myItem) in order to check if a property on an item in the list equals a property on myItem.

(We have considered Contains and Exists, something like:

if (orderLines.Contains(myLine)) { ... }

but cannot get a simple expression.)

We would like something as simple as the following:

if (orderLines.[MethodName](myLine))
{
}

and this method should automagically return true if at least one item orderLines[i] fulfills:

myLine.Product.Equals(orderLines[i].Product)

We have implemented IEquatable<OrderLine> which equates by the Product property, but cannot find how to proceed from there.

Ole Lynge
  • 4,457
  • 8
  • 43
  • 57

1 Answers1

6

Are you using .NET 3.5? If so, use LINQ:

if (orderLines.Any(order => order.Product == myLine.Product))
{
    ...
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194