What is Lambda Expression?
Lambda Expressions, using the Lambda Operator => were introduced in C# 3.0.A lambda expression is an anonymous function or simply a method that can contain expressions and statements, and can be used to create delegates or expression tree types. All lambda expressions use the lambda operator =>, which is read as “goes to”. The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read “x goes to x times x.”
We can use lambdas in method based LINQ query as argument to standard query. We cannot use lambda operators on the left side of is or as operator.
Let see following example of Anonymous Methods and Lambda Expression.
Step 1
Create a Console Application and give the solution name as ConLambdaExpression.
Step 2
Create a Developer Class in the solution,it's look like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConLambdaExpression
{
public class Developer
{
#region Property
public String DeveloperName
{
get;
set;
}
public String Speciality
{
get;
set;
}
public int Experience
{
get;
set;
}
#endregion
}
}
Step 3
Now add developer data in to list object in Main function,it is look like this
List<Developer> DeveloperObj = new List<Developer>();
// Add Developer Data to the list object
DeveloperObj.Add(new Developer()
{
DeveloperName = "Bhushan Pawar",
Speciality = "ASP.net",
Experience = 5
});
DeveloperObj.Add(new Developer()
{
DeveloperName = "Ramdas Bhosale",
Speciality = "ASP.net",
Experience = 4
});
DeveloperObj.Add(new Developer()
{
DeveloperName = "Koste Budinoski",
Speciality = "ASP.net MVC",
Experience = 7
});
DeveloperObj.Add(new Developer()
{
DeveloperName = "Kishor Naik",
Speciality = "WPF & Silverlight",
Experience = 4
});
DeveloperObj.Add(new Developer()
{
DeveloperName="Vikas Bharti",
Speciality="SQL Server",
Experience=7
});
DeveloperObj.Add(new Developer()
{
DeveloperName="Shashikant Patil",
Speciality="ASP.net",
Experience=3
}
);
DeveloperObj.Add(new Developer()
{
DeveloperName = "Shilpa Gawde",
Speciality="WPF",
Experience=3
}
);
Step 4
Anonymous Methods
C# 2.0 provides a new feature called Anonymous Methods, which allow you to create inline un-named ( i.e. anonymous ) methods in your code, which can help increase the readability and maintainability of your applications by keeping the caller of the method and the method itself as close to one another as possible.
To find a data from list object using Anonymous Methods,it's look like this
// Search Data using Anonymous Method
var AnonymousQuery = DeveloperObj.Where(
delegate(Developer D)
{
return D.DeveloperName.ToLower().Contains("vikas");
}
);
System.Console.WriteLine("Anonymous Method");
foreach (Developer DObj in AnonymousQuery)
{
System.Console.WriteLine("Developer Name\t:\t" + DObj.DeveloperName);
System.Console.WriteLine("Speciality\t:\t" + DObj.Speciality);
System.Console.WriteLine("Experience\t:\t" + DObj.Experience);
}
System.Console.WriteLine();
Step 5
To find data from list object using Lambda Expression,it's look like this
//Search Data Using Lamda Expression
var LambdaQuery = DeveloperObj.Where(LE => LE.DeveloperName.ToLower().Contains("ramdas"));
System.Console.WriteLine("Lambda Expression");
foreach (Developer DObj in LambdaQuery)
{
System.Console.WriteLine("Developer Name\t:\t" + DObj.DeveloperName);
System.Console.WriteLine("Speciality\t:\t"+DObj.Speciality);
System.Console.WriteLine("Experience\t:\t"+DObj.Experience);
}
Run the Project.
Output
Click on Image for better View
Full Main Function Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConLambdaExpression
{
class Program
{
static void Main(string[] args)
{
try
{
List<Developer> DeveloperObj = new List<Developer>();
// Add Developer Data to the list object
DeveloperObj.Add(new Developer()
{
DeveloperName = "Bhushan Pawar",
Speciality = "ASP.net",
Experience = 5
});
DeveloperObj.Add(new Developer()
{
DeveloperName = "Ramdas Bhosale",
Speciality = "ASP.net",
Experience = 4
});
DeveloperObj.Add(new Developer()
{
DeveloperName = "Koste Budinoski",
Speciality = "ASP.net MVC",
Experience = 7
});
DeveloperObj.Add(new Developer()
{
DeveloperName = "Kishor Naik",
Speciality = "WPF & Silverlight",
Experience = 4
});
DeveloperObj.Add(new Developer()
{
DeveloperName="Vikas Bharti",
Speciality="SQL Server",
Experience=7
});
DeveloperObj.Add(new Developer()
{
DeveloperName="Shashikant Patil",
Speciality="ASP.net",
Experience=3
}
);
DeveloperObj.Add(new Developer()
{
DeveloperName = "Shilpa Gawde",
Speciality="WPF",
Experience=3
}
);
// Search Data using Anonymous Method
var AnonymousQuery = DeveloperObj.Where(
delegate(Developer D)
{
return D.DeveloperName.ToLower().Contains("vikas");
}
);
System.Console.WriteLine("Anonymous Method");
foreach (Developer DObj in AnonymousQuery)
{
System.Console.WriteLine("Developer Name\t:\t" + DObj.DeveloperName);
System.Console.WriteLine("Speciality\t:\t" + DObj.Speciality);
System.Console.WriteLine("Experience\t:\t" + DObj.Experience);
}
System.Console.WriteLine();
//Search Data Using Lamda Expression
var LambdaQuery = DeveloperObj.Where(LE => LE.DeveloperName.ToLower().Contains("ramdas"));
System.Console.WriteLine("Lambda Expression");
foreach (Developer DObj in LambdaQuery)
{
System.Console.WriteLine("Developer Name\t:\t" + DObj.DeveloperName);
System.Console.WriteLine("Speciality\t:\t"+DObj.Speciality);
System.Console.WriteLine("Experience\t:\t"+DObj.Experience);
}
}
catch (Exception ex)
{
System.Console.WriteLine();
}
}
}
}
Download
Download Source Code