Free Web Development Online Tutorials, Learn to Code
Lambda Expression C# Examples, GroupBy, OrderBy Lambda Expression in C#, lambda expression c# tutorial

Lambda Expressions C# Example

Here you learn how to write Lambda expressions in C#, Lambda expressions are type of anonymous functions that contain sequence of operators, which are termed as expressions

Lambda Expressions in LINQ C#

Why Lambda?
Using Lambda expression we can write function body without writing a delegate function, Very useful while writing any anonymous methods

var studentsStream = new List<Student> { 
    new Student { FullName = "Aruna", StreamId=1,  Score = 10 },
    new Student { FullName = "Janet", StreamId=2,  Score = 9  },
    new Student { FullName = "Ajay", StreamId=1,  Score = 11 },
    new Student { FullName = "Kunal", StreamId=2,  Score = 13  },
    new Student { FullName = "Chandra", StreamId=2,  Score = 8  },

Example 1: from above student list we want to select only student names

lambda select one column

c# Lambda expression select example.

// Way 1 : (Query syntax)
var studentNames = from s in studentsStream select s.FullName;

//Way 2: Using Lambda Expression var _studentNames = studentsStream.Select<Student, string>(s => s.FullName);
//Way 3 var _studentNames1 = studentsStream.Select(s => s.FullName).ToArray();

Linq lambda conditional where

Example 2: from above student list select students where score is more than 10

c# lambda select where in list
// Way 1
var studentList = from stu in studentsStream
where stu.Score>10 select stu;

// Way 2: Using Lambda Expression var studentList1 = studentsStream.Where<Student>(stu => stu.Score> 10);
// Way 3 var studentList2 = studentsStream.Where(s => s.Score > 10).ToList<Student>();

linq lambda group by multiple

Here is an example of how you can use group by multiple columns in linq lambda expression

// Way 1
var students =
from s in studentsStream
group s by new { s.StreamId, s.Score } into g
orderby g.Key.StreamId
select new { StreamId = g.Key.StreamId, Score = g.Key.Score, TotalCount = g.Count() };

// Way 2: Using Lambda Expression var students1 = studentsStream .GroupBy(s => new { s.Score, s.StreamId}) .OrderBy(g => g.Key.StreamId) .Select(g => new { StreamId = g.Key, TotalCount = g.Count() });

linq lambda order by

Here in below example you can see we have used order by to read different columns into a Ienumerable variable which is anonymous type

// Ienumerable variable
var students1 =
.OrderBy(g => g.FullName)
.Select(g => new { Name = g.FullName, Stream = g.StreamId, TotalScore = g.Score });

// Ienumerable anonymous type foreach (var s in students1) { Console.WriteLine(string.Format("{0}-{1}-{2}", s.Name,s.Stream, s.TotalScore)); }

Lambda Expression Builder

We can also build lambda expressions dynamically using expression builder.

For example, if we want to select students from above collection object, where StreamId is less than or equal to 10.

var students = studentsStream.Where(stu => stu.StreamId <= 10)

Now, we write the same above query, using query builder like example below.

To use expression builder you need to use namespace reference System.Linq.Expressions;

using System.Linq.Expressions;

// We can build query like 
var parameter = Expression.Parameter(typeof(Student), "stu");

var member = Expression.Property(parameter, "StreamId"); //stu.StreamId

var constant = Expression.Constant(10);

var body = Expression.LessThanOrEqual(member, constant); //stu.StreamId <= 10

var finalExpression = Expression.Lambda<Func<Student, bool>>(body, parameter);
Learn LINQ .Net

Lambda Expression in C# Example, LINQ Lambda Expression

linq Interview Questions Answers

LINQ C# examples | Join MVC Course