A overview of C# lambda expressions

Facebook
Twitter
LinkedIn

Never miss a post!

Sign up for our newsletter and get FREE Development Trends delivered directly to your inbox.

You can unsubscribe any time. Terms & Conditions.
Categories

In C#, Lambda Expressions are anonymous functions that are used to express a series of expressions or a sequence of operators. Typically, all the lambda expressions use the special lambda operator ‘=>’, which can be read as “becomes” or “goes to”. The lambda expression is divided into two parts i.e. the first part ( or the left ) part is the input and the second part ( or right part ) is the expression or a block of code, which works as an entry parameter. There is no need to specify the type of input in the lambda expression, which is one of the reasons behind its simple implementation. The lambda expressions are used as an alternate option to delegates.

Types of Lambda Expressions

The lambda expression can have two types:

  1. Expression Lambda

The Expression Lambda consists of the input and the expression. You can write it as it is given below

input => expression;

  1. Statement Lambda

The Statement Lambda consists of the input and a set of operations or statement to be executed. You can write it as it is given below

input => { statements };

 

Using Joins in Lambda

The Joins are used to merge one or more than two tables into a single unit. It takes two sources as input and produces a combine single output.

Let us take an example of students that belong to different departments and you can fetch the list of students along with the department names by using any of the two different ways.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mylambdauniversityapp {

public class Department{
   public int DepartmentID { get; set; }
   public string DepartmentName { get; set; }
   public static List GetDepartmentList(){
      return new List(){
         new Department { DepartmentID = 1, DepartmentName = "Computer Science"},
         new Department { DepartmentID = 2, DepartmentName = "Management Sciences"},
      };
   }
}

public class Students{
   public int StudentID { get; set; }
   public string StudentName { get; set; }
   public int DepartmentID { get; set; }
   public static List GetStudentList(){
      return new List(){
         new Student { StudentID = 1, StudentName = " Student A", DepartmentID = 1 },
         new Student { StudentID = 2, StudentName = " Student B", DepartmentID = 2 },
         new Student { StudentID = 3, StudentName = " Student C", DepartmentID = 1 },
         new Student { StudentID = 4, StudentName = " Student D", DepartmentID = 1 },
      };
   }
}
public class Program{   
      public static void Main (string[] args) 
      {      
         // fetch a list of students with the departments
         var result = Student.GetStudentList().Join( Department.GetDepartmentList(),
         s => s.DepartmentID,
         d => d.DepartmentID, ( Student, department ) => new{
            StudentName = Student.StudentName,
            DepartmentName = department.DepartmentName
         });
         foreach (var Student in result) {
            Console.WriteLine( " Student Name: " +  student.StudentName + " \t " + " Department Name: " + student.DepartmentName );
         }
         var result1 = from s in Student.GetStudentList()
         join d in Department.GetDepartmentList()
         on s.DepartmentID equals d.DepartmentID
         select new {
            StudentName = s.StudentName,
            DepartmentName = d.DepartmentName
         };
         foreach (var student in result1) {
             Console.WriteLine( " Student Name: " + student.StudentName + " \t " + " Department Name: " + Student.DepartmentName);
         }
         Console.ReadLine();
      }
   }
}

Using GroupBy in Lambda

The GroupBy condition displays list as a group separated by a key.

public static void Main (string[] args)
{
    int[] numberArray = { 4, 7, 8, 10, 11, 15 };

    var result = numberArray.GroupBy(n => (n % 2 == 0));

    // using GroupBy condition to create two different groups
    foreach (IGrouping<bool, int> group in result)
    {
        if (group.Key == true)
            Console.WriteLine(" Numbers divisible by 2 ");
        else
            Console.WriteLine(" Numbers not divisible by 2 ");

        foreach (int number in group)
            Console.WriteLine(number);
    }
}
Facebook
Twitter
LinkedIn

Our website uses cookies that help it to function, allow us to analyze how you interact with it, and help us to improve its performance. By using our website you agree by our Terms and Conditions and Privacy Policy.