Posts

Showing posts with the label LINQ

LINQ Operators and Lambda Expression - Syntax & Examples

Image
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.Co