Predicates and Lambdas with lists and collections.


Junior Member
Feb 1, 2010
Reaction score
Environment: Visual Studios 2008 using VB

(this is also valid for c# although the syntax is slightly different)

i just learned a nifty technique that you can apply to working with collections and lists. this is kind of an extension of the linq concept, it uses predicates and lambdas to accomplish what you want. this can be used with several different methods associated with lists and collections, i specifically needed this for a remove all. i'm using a list of integers for simplicity, but this applies to properties and values for any class or object.
in c# you can use anonymous delegates in addition to lambdas (in vb you need explicit delegates). :)

[SIZE=2]Dim theList As New List(Of Integer)[/SIZE] 
[SIZE=2]Debug.WriteLine("Pre-Count: " & theList.Count.ToString())[/SIZE]  
[SIZE=2]' witchcraft![/SIZE]  
[SIZE=2]theList.RemoveAll(Function(a) a = 3)[/SIZE]  
[SIZE=2]Debug.WriteLine("Post-Count: " & theList.Count.ToString())[/SIZE]
here is a slightly different example using the properties of a user defined class:

' find the object in the collection that we need.
' ToVerify is a type specific list
Current = (From x In ToVerify _
                        Where x.ID = 5 _
                        Select x).First
' set up your predicate expression with a lambda.
ToVerify.RemoveAll(Function(A) A.ID = 5)
along with RemoveAll here is a short list of some of the other methods this works with:

some related links:

just thought this was cool and wanted to share it, this may be one of those things where once again i am the last one to learn about it. ;)

like greek statistics :D

Nice post OP. :)
this has become one of my new favorite tricks for working with lists or collections of any type. it's very powerful, flexible, and compact.

my only real gripe is that if it doesn't find what it is looking for it throws a Linq exception telling you there is nothing there. personally i would prefer to have it return nothing or a null value and then evaluate for that, but at least the exception is specific enough that you can trap for it and handle accordingly.

this knocks the socks off having to iterate through a list and maintain indexes.

as an aside you'll notice at the end of the linq query i am using .First to get the first instance in the result set of my match. there are several other things that you can do with this as well, some of which being:


etc etc etc...

it pretty much extends to you the ability to do anything you can in sql, just in code. :)

the extension you use (.First, etc...) determines how your object variable needs to be dimensioned.


[SIZE=2]Dim theList As New List(Of Integer)[/SIZE] 

Dim current As List(Of Integer)
current = From x In [SIZE=2]theList[/SIZE]_
             Where x <> 3 _
             Select x).ToList


all kinds of power wrapped up in this.
ok, one last thing about this and then i'm done. ;)

here is a quick example on how to do use the lambda for the .ToDictionary function. essentially you use the lambda as a directive to tell the function what to use as the key.

Dim MyDictionary As Dictionary(Of MyObjects) = MyObjectList.ToDictionary(Function(e) e.PropertyToBeTheKey)



.... seriously, I have always just glanced over any talk of Lambdas, and never googled anything about them... :) thanks for the info
haha, anytime davinci. :)

i always love to learn new little tricks and techniques like this, and when i can like to pass them along. sometimes i find these concepts hard to learn in an abstract manner, so if i can find a good tangible way to explain it, it helps re-enforce it in my own mind.

at any rate, glad you liked the post. cheers!
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features and essential functions on BlackHatWorld and other forums. These functions are unrelated to ads, such as internal links and images. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock