views:

204

answers:

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.

+5  A: 

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

if (orderLines.Any(order => order.Product == myLine.Product))
{
    ...
}
Jon Skeet