SIGN UP MEMBER LOGIN:    
ARTICLE

One Line Lambda-Lookup list of Lambda Expressions for common scenarios

Posted by Jean Paul Articles | Current Affairs December 08, 2010
I have observed that Lambda Expressions got good steam among developers. It helps us in reducing lot of code and save time.
Reader Level:

I have observed that Lambda Expressions got good steam among developers. It helps us in reducing lot of code and save time.

I would like to list some of the useful Lambda Expressions here comparing with the without-lambda approach.

Common Setup Code

Here the common initialization code for using the below examples are given:

List<int> list = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10});

Scenario Lambda Approach Traditional Approach
Descending Sort list.OrderByDescending(i => i);

list.Sort(delegate(int x, int y)
    {
       
return y.CompareTo(x);
    });

Check all numbers are less than 100 bool result = list.All(i => i < 100);

bool result = true;
foreach (int i in list)
   
if (i >= 100)

        result = false;
Check any numbers are equal to 5 bool result = list.Any(i => i == 5);

bool result = false;
foreach (int i in list)
   
if (i == 5)

        result = true;
Get the top 5 items in the list var sublist = list.Take(3);

var sublist = new List<int>();
for (int i = 0; i < 5; i++)
   
if (i < list.Count)
        sublist.Add(i);

Sort descending, get all numbers less than 8, take the top 5 var result = list.OrderByDescending(i => i).Where(i => i < 8).Take(5);

list.Sort(delegate(int x, int y)

   
return y.CompareTo(x); 
});

List<int> result = new List<int>();

foreach (int i in list)

    if (i < 8)

        if (result.Count < 5)

            result.Add(i);

Summary

I think the examples above demonstrate how complex the code will look even if you do the simple sorting and selection.

Surely Lambda Expressions is worth learning and in the long run it would give more manageable code.

Note

In the sorting scenario above, the original list is not updated, OrderBy() will be giving a sorted view of the original list.

For assigning it back to the original list we have to use the ToList<int>() method as:

list = list.OrderByDescending(i => i).ToList<int>();

Login to add your contents and source code to this article
share this article :
post comment
 

Hello Vinu, You can use OrderByDescending() in the case you wanted a descending order of elements. Eg: Let your list is 1 2 3 OrderByDescending() makes the list as 3 2 1 Regards, Jean Paul

Posted by Jean Paul Jul 20, 2011

Hi, I am a newbie to Lambda expressions. Can you please explain where and how to add the OrderByDescending function? Thanks, Vinu

Posted by Vinu Vishnu Jul 20, 2011

Hi Jean, It's really very useful for quick reference about Lamda Expressions. Keep stay on posting more articles. Regards, M.S.Haq

Posted by Jean Paul Dec 09, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor