Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > ASP.NET 4.5 General Discussion
|
ASP.NET 4.5 General Discussion For ASP.NET 4.5 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4.5 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 8th, 2013, 08:24 AM
Authorized User
 
Join Date: Dec 2011
Posts: 26
Thanks: 1
Thanked 4 Times in 3 Posts
Default Why use lambda expression with LINQ in this case - MVC 4

Hi,
I am reading through some materials on ASP.NET MVC 4. In the example, they want to sort through some "product" objects, and grab the 3 with the highest prices. They are using LINQ's dot notation to do it. Here is the LINQ dot notation they give.


Code:
var foundProducts = products.OrderByDescending(e => e.Price)
     .take(3)
     .Select(e => new {
          e.Name
          e.Price
      });
My question involves the lambda expressions used. Maybe I do not understand them well enough yet, but I do not see why they are needed in this case. Why can you not do the following?

Code:
var foundProducts = products.OrderByDescending(e.Price)
     .take(3)
     .Select(new {
          e.Name
          e.Price
      });
It might be a dumb question because I am not sure how the system knows what e is without the lambda, but can someone please explain that (why it is needed) to me, and/or explain to me why the lambdas are useful here?

Thanks in advance
 
Old August 14th, 2013, 02:36 PM
Authorized User
 
Join Date: Jun 2013
Posts: 36
Thanks: 1
Thanked 4 Times in 4 Posts
Default Lambdas

You need the lambda-expression here.
Lamba is a shorthand form for calling anonymous methods.

It comes from assigning anonymous methods as handlers for events:
Code:
SubmitButton.Click += delegate (object sender, EventArgs e)
    { MessageBox.Show("Click!") };
Here we have an anonymous method (i.e. method without a name) that will be called that Click-event fires.

Another way to write it is with lambdas:
Code:
SubmitButton.Click += (s, e) => MessageBox.Show("Click!");
"s" and "e" are parameters (object sender & EventArgs e); the compiler can infer them from the "Click"-event and does not need details.
"=>" - is the lambda that means "goes to". Parameters "s" and "e" go into this method. We can use "MessageBox(s.SomeProperty)" because "s" stands for incoming object.

"OrderByDescending()"-method expects a delegate Func<TSource, TKey> and writing a lambda-expression actually provides this TSource. (Func is a special built-in delegate in .NET)
It has nothing to do with ASP.NET MVC - it is the same for all LINQ queries.

If all that sounds rather confusing, you may either just ignore it, or dig up a book on C# and read about delegates, lambdas, extension methods and LINQ.

Last edited by Oleg Kolpashchikov; August 16th, 2013 at 03:57 PM.. Reason: added [CODE]-formatters
 
Old August 15th, 2013, 09:43 PM
Authorized User
 
Join Date: Aug 2012
Posts: 39
Thanks: 4
Thanked 1 Time in 1 Post
Default

Interesting. Good reply!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
'Double DistanceBetween..' cannot be translated into a LINQ to Expression flyinhawaiian BOOK: Professional ASP.NET MVC 2 8 November 6th, 2014 06:26 AM
Chapter 14 Lambda Expression as Delegates and Expression Trees TJinWI BOOK: Beginning Visual C# 2012 1 July 16th, 2013 10:50 AM
Chapter 14 Lambda Expression as Delegates and Expression Trees TJinWI BOOK: Beginning Object-Oriented Programming with C# 3 July 16th, 2013 10:05 AM
Regular Expression Validator - odd case? nick1248 BOOK: Beginning ASP.NET 4 : in C# and VB 1 May 11th, 2013 06:15 AM
LINQ query \ LAMBDA expression - complex requirement sumitshah4u .NET Framework 3.5 0 March 16th, 2009 04:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.