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
IEnumerable
from p in products
where p.UnitPrice >= 10
select p;
Select
IEnumerable
IEnumerable
var namesAndPrices =
products.
Where(p => p.UnitPrice >= 10).
Select(p => new { p.Name, p.UnitPrice }).
ToList();
IEnumerable
products.
Select((product, index) => new { product, index }).
Where(x => x.product.UnitPrice >= 10).
Select(x => x.index);
SelectMany
IEnumerable
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
products.OrderByDescending(p => p.UnitPrice).Take(10);
Skip
IEnumerable
products.OrderByDescending(p => p.UnitPrice).Skip(10);
TakeWhile SkipWhile
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 };
GroupJoin
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
customers.Select(c => c.City).
Concat(customers.Select(c => c.Region)).
Concat(customers.Select(c => c.Country)).
Distinct();
IEnumerable
new[] {
customers.Select(c => c.City),
customers.Select(c => c.Region),
customers.Select(c => c.Country),
}.
SelectMany(s => s).
Distinct();
OrderBy / ThenBy
IEnumerable
products.
OrderBy(p => p.Category).
ThenByDescending(p => p.UnitPrice).
ThenBy(p => p.Name);
IEnumerable
from p in products
orderby p.Category, p.UnitPrice descending, p.Name
select p;
IEnumerable
products.
Where(p => p.Category == "Beverages").
OrderBy(p => p.Name, StringComparer.CurrentCultureIgnoreCase);
IEnumerable
products.
Where(p => p.Category == "Beverages").
Select(p => p.Name).
OrderBy(x => x);
GroupBy
IEnumerable
products.GroupBy(p => p.Category);
IEnumerable
products.GroupBy(p => p.Category, p => p.Name);
Distinct
IEnumerable
products.Select(p => p.Category).Distinct();
AsEnumerable
Table
var query = custTable.AsEnumerable().Where(c => IsGoodCustomer(c));
ToArray
string[] customerCountries =
customers.Select(c => c.Country).Distinct().ToArray();
ToList
List
customers.
Where(c => c.Orders.Any(o => o.OrderDate.Year == 2005)).
ToList();
ToDictionary
Dictionary
customers.
SelectMany(c => c.Orders).
Where(o => o.OrderDate.Year == 2005).
ToDictionary(o => o.OrderID);
Dictionary
products.
GroupBy(p => p.Category).
ToDictionary(g => g.Key, g => g.Group.Max(p => p.UnitPrice));
ToLookup
Lookup
products.ToLookup(p => p.Category);
IEnumerable
OfType
List
IEnumerable
Cast
ArrayList objects = GetOrders();
IEnumerable
objects.
Cast
Where(o => o.OrderDate.Year == 2005);
ArrayList objects = GetOrders();
IEnumerable
from Order o in objects
where o.OrderDate.Year == 2005
select o;
First
string phone = "206-555-1212";
Customer c = customers.First(c => c.Phone == phone);
Single
int id = 12345;
Customer c = customers.Single(c => c.CustomerID == id);
ElementAt
Product thirdMostExpensive =
products.OrderByDescending(p => p.UnitPrice).ElementAt(2);
Range
int[] squares = Enumerable.Range(0, 100).Select(x => x * x).ToArray();
Repeat
long[] x = Enumerable.Repeat(-1L, 256).ToArray();
Empty
IEnumerable
Any
bool b = products.Any(p => p.UnitPrice >= 100 && p.UnitsInStock == 0);
All
IEnumerable
products.
GroupBy(p => p.Category).
Where(g => g.Group.All(p => p.UnitsInStock > 0)).
Select(g => g.Key);
Count
int count = customers.Count(c => c.City == "London");
Sum
int year = 2005;
var namesAndTotals =
customers.
Select(c => new {
c.Name,
TotalOrders =
c.Orders.
Where(o => o.OrderDate.Year == year).
Sum(o => o.Total)
});
Min
var minPriceByCategory =
products.
GroupBy(p => p.Category).
Select(g => new {
Category = g.Key,
MinPrice = g.Group.Min(p => p.UnitPrice)
});
Max
decimal largestOrder =
customers.
SelectMany(c => c.Orders).
Where(o => o.OrderDate.Year == 2005).
Max(o => o.Total);
Average
var averageOrderTotals =
customers.
Select(c => new {
c.Name,
AverageOrderTotal = c.Orders.Average(o => o.Total)
});
Aggregate
products.
GroupBy(p => p.Category).
Select(g => new {
Category = g.Key,
LongestName =
g.Group.
Select(p => p.Name).
Aggregate((s, t) => t.Length > s.Length ? t : s)
});
http://www.c-sharpcorner.com/uploadfile/babu_2082/linq-operators-and-lambda-expression-syntax-examples