tags:

views:

219

answers:

3
+1  Q: 

Lambda Example

I am still learning some of the features of C# 3.0 and want to know if the following can be reduced to a lambda expression.

var SomeObject = Combo.EditValue;
var ObjectProperty = SomeObject.Property;

To obtain ObjectProperty from the combo.editvalue in a single line?

Also, if you can provide me with any good references to Lambda expressions, it would be appreciated.

EDIT: Ok, the answers posted are great, it appears that the example does not need a Lambda to satisfy the solution. I will take a look at the reference links though ... many thanks to those that contributed.

+3  A: 

You don't really need lambdas to do that all you would need to do is

var ObjectProperty = Combo.EditValue.Property;

I'm not sure a lambda is going to make that any more readable for you.

Here are some books you might want to take a look at to learn Lambdas in more detail, and also why you'd use them:

More Effective C#

C# In Depth

MSDN Reference

Joseph
I was thinking the same thing and trying to see if there was any nuance I was missing in the question. Looks reasonable though :)
workmad3
I was about to post the same thing.
JohnOpincar
Yeah I'm still looking for a good lambda reference for the guy, though
Joseph
Manning publications has a book on Lambda in Depth, I believe that should be good, as I have been impressed with the other books I have gotten from them.
James Black
A: 

Combining those into one line, you run the risk of a NullReferenceException, by checking the Property property on EditValue. :) But, here is a really great tutorial on C# 3.0 and functional programming.

JP Alioto
There's a risk of a NullReferenceException when the code is on two lines as well, unless you add in some extra code to check first ...
codeulike
He ran the risk anyway, he wasn't checking the first place =P He would need to check either way he went.
Joseph
It's true, I just didn't like the way Combo.EditValue.Property looked on the page, so I skipped it. :)
JP Alioto
A: 

this does not appear to need a lambda.
Can't you just use

var ObjectProperty = Combo.EditValue.Property

As far as lambda references try 101 LINQ Examples for starters.

Jeremy