Contact Info

sean [at] coreitpro [dot] com gpg key

sc68cal on Libera

Using Linq And Lambda Functions

How to use LINQ, Anonymous Functions, and Lambda Expressions

	var results = myStackPanel.Children.OfType<TextBox>().Where(x => x.Text == "");
	if (results.Count() >0)
	{
		//Something is blank
	}

areyouawizard

How it works

You may ask: “ what the heck is the “=>” ?

That’s part of the lambda expression. Here’s what happens. Imagine we have a collection of 6 textboxes.

 0x0 {Textbox}
 0x1 {Textbox}
 0x2 {Textbox}
 0x3 {Textbox}
 0x4 {Textbox}
 0x5 {Textbox}
 0x6 {Textbox}

The lambda expression ( x => x.Text == “” ) expands to something like this:

     bool function(Textbox x){
	     return x.Text == ""
     }

Now, the Children.OfType<TextBox>().Where() function calls the function above on every textbox in the collection.