LINQ Operators and Lambda Expressions
LINQ is a cool feature in C# 3.0. Most of the developers are struggling for the syntax and examples. Here I have collected various examples for each operator in LINQ and the equivalent Lambda Expressions.
Where
- IEnumerable<Product> x = products.Where(p => p.UnitPrice >= 10);
- IEnumerable<Product> x =
- from p in products
- where p.UnitPrice >= 10
- select p;
Select
- IEnumerable<string> productNames = products.Select(p => p.Name);
- IEnumerable<string> productNames = from p in products select p.Name;
- var namesAndPrices =
- products.
- Where(p => p.UnitPrice >= 10).
- Select(p => new { p.Name, p.UnitPrice }).
- ToList();
- IEnumerable<int> indices =
- products.
- Select((product, index) => new { product, index }).
- Where(x => x.product.UnitPrice >= 10).
- Select(x => x.index);
SelectMany
- IEnumerable<Order> orders =
- customers.
- Where(c => c.Country == "Denmark").
- SelectMany(c => c.Orders);
- var namesAndOrderIDs =
- customers.
- Where(c => c.Country == "Denmark").
- SelectMany(c => c.Orders).
- Where(o => o.OrderDate.Year == 2005).
- Select(o => new { o.Customer.Name, o.OrderID });
- var namesAndOrderIDs =
- customers.
- Where(c => c.Country == "Denmark").
- SelectMany(c => c.Orders, (c,o) => new { c, o }).
- Where(co => co.o.OrderDate.Year == 2005).
- Select(co => new { co.c.Name, co.o.OrderID });
- var namesAndOrderIDs =
- from c in customers
- where c.Country == "Denmark"
- from o in c.Orders
- where o.OrderDate.Year == 2005
- select new { c.Name, o.OrderID };
Take
- IEnumerable<Product> MostExpensive10 =products.OrderByDescending(p => p.UnitPrice).Take(10);
- IEnumerable<Product> AllButMostExpensive10 = products.OrderByDescending(p => p.UnitPrice).Skip(10);
- s.TakeWhile(p) s.SkipWhile(p)
Join
- var custOrders = customers.
- Join(orders, c => c.CustomerID, o => o.CustomerID, (c, o) => new {
- c.Name, o.OrderDate, o.Total
- });
- var custOrders = from c in customers
- join o in orders on c.CustomerID equals o.CustomerID
- select new {
- c.Name, o.OrderDate, o.Total
- };
- var custTotalOrders = customers.
- GroupJoin(orders, c => c.CustomerID, o => o.CustomerID, (c, co) => new {
- c.Name, TotalOrders = co.Sum(o => o.Total)
- });
- var custTotalOrders = from c in customers
- join o in orders on c.CustomerID equals o.CustomerID into co
- select new {
- c.Name, TotalOrders = co.Sum(o => o.Total)
- };
- var custTotalOrders = from c in customers
- join o in orders on c.CustomerID equals o.CustomerID
- select new {
- c.Name, o.OrderDate, o.Total
- };
- var custTotalOrders = from c in customers
- join o in orders on c.CustomerID equals o.CustomerID into co
- from o in co
- select new {
- c.Name, o.OrderDate, o.Total
- };
- var custTotalOrders = from c in customers
- join o in orders on c.CustomerID equals o.CustomerID into co
- from o in co.DefaultIfEmpty(emptyOrder)
- select new {
- c.Name, o.OrderDate, o.Total
- };
Concat
- IEnumerable < string > locations = customers.Select(c => c.City).
- Concat(customers.Select(c => c.Region)).
- Concat(customers.Select(c => c.Country)).
- Distinct();
- IEnumerable < string > locations = new [] {
- customers.Select(c => c.City),
- customers.Select(c => c.Region),
- customers.Select(c => c.Country),
- }.
- SelectMany(s => s).
- Distinct();

Mayooran NavamanyPosted Aug 9, 2022, 10:19 AM
Good one thannks
Rajanikant HawaldarPosted Aug 20, 2019, 5:05 AM
Nice article
saeed kPosted Jan 17, 2019, 3:57 AM
More useful if show output from every exampe
Raghav PPosted Sep 1, 2017, 1:19 AM
Nice article, Plz share some basic step by step query writing in linq for CRUD operation.Thanks in advance.
Truong Minh TuanPosted Jul 26, 2017, 11:43 AM
Very nice for article of Manikavelu Velayutham. Hope the features, me try more articles...!
Sonu ChaudharyPosted Jun 7, 2016, 12:33 PM
good one thannks
Mukesh Kumar TiwariPosted Dec 10, 2014, 6:26 AM
Very Nice...
SatyaPosted Oct 19, 2014, 8:03 AM
great work..really helpful for the new developers.Thanks
Santosh YadavPosted May 2, 2013, 12:58 PM
Very good post but not for beginners.
Siva Kumar ssPosted Feb 15, 2013, 5:38 AM
Very Useful and Handy.....
Lajapathy ArunPosted Sep 13, 2012, 3:28 AM
Nice and practical approach :)
Farooq MdPosted Oct 25, 2011, 11:07 AM
Great, Thanks