Wednesday 7 January 2009

C# .NET Simple Predicate Example

Someone asked me this question the other day, and I had to really think hard about the answer, so I'm going to blog it. That way next time I need to know, I'll have somewhere to look right away - and maybe it'll be useful to someone else!

Assume you have a class "Product" with a property called "SKU".

You have a strongly typed generic collection e.g. List and want to find a matching instance inside it:

string skuToFind = "SKU2";

Product matchingProduct = myCollection.Find(new delegate(Product current) { return current.SKU = skuToFind; });


There you go!

0 comments: