Tuesday, November 27, 2007

How to use the SubSonic Collection Find() method with Predicate

Here is a code example to find a particular item within a collection of entities.




Instead of creating a loop to go over the collection to find your entity, you can take advantage of the Predicates.

Model.Person foundPerson =(Model.Person) personCollection.Find(delegate(Model.Person e) { return e.FirstName == "John"; });




I hope this is helpful. I did not find anything in the SubSonic forums regarding the Find method. This worked for me instead of creating a loop to go over all items in the collection.

2 comments:

  1. Thanks for sharing this! Very helpfull.

    ReplyDelete
  2. A great example. I'm am running into situation where I need a composite key on a collection (where the primary key is not relevant) I'm having hard time passing two keys without having to create a class (which seems a bit too much work when you can convert it to a table and query the table...hmmmm?

    ReplyDelete