Sean Collins

sean [at] seanmcollins [dot] com

GPG Key ID: 0xf60f564978913931

sean [at] coreitpro [dot] com

GPG Key ID: 0xA1D7E590

profile for Sean at Stack Overflow, Q&A for professional and enthusiast programmers

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.