Click here to Skip to main content
14,768,428 members
Articles » Platforms, Frameworks & Libraries » .NET Framework » General
Posted 28 Feb 2009


263 bookmarked

Basics of LINQ & Lamda Expressions

Rate me:
Please Sign up or sign in to vote.
4.76/5 (134 votes)
28 Feb 2009CPOL
Basic Examples to work with Language Integrated Query(LINQ) concept that was introduces with C# 3.0 and/or .NET 3.5 (Corresponding VB.NET code included)
  1. Introduction
  2. What is LINQ
  3. Types of LINQ.
  4. Lambda Expression
  5. Extension Methods
  6. What is Yield?
  7. Annonymous Types
  8. Meaning of VAR 
  9. Demonstration of Linq (With Example)
  10. Operators :
  13. Reference 
  14. Conclusion
  15. History 


Hi Friends, Its a long time since I have last written a Article. Since then, I have learned a lot of things and its time to share them with you. .NET is getting richer and richer with new concepts. The most popular amongst all of them is the introduction of Query Integration on basic Application Data.  


Linq is the Microsoft's first attempt to integrate queries into language. We know, it is really easy to find data from sql objects simply writing a query while its somewhat hectic when we want to do the same thing in a DataTable or Lists. Generally we will have to loop through every elements to find the exact match, if there is some aggregation we need to aggregate the values etc. Linq provides an easy way to write queries that can run with the in memory objects.  

Types of LINQ 

Linq Comes with 3 Basic Types (Provided there are lots of more types of LINQ on different type of objects :

1. LINQ (Linq to Objects)
2. DLINQ (Linq to SQL)
3. XLINQ (Linq to XML)

Before we continue with LINQ, we must look at some of the new inclusions which may come to us very handy.

Lambda Expression 

Linq is always associated with Lambda Expressions. In .NET 2.0 we have the concept of Annonymous Methods that allows you to write function body inline without the need of writing a delegate function. Lambda Expression of .NET 3.5 is to consize the concept of Annonymous function writing.
Let us take a look of how to write Annonymous methods in C# 2.0.

int i = this.compute(10);
private int compute(int value) 
   return (value + 2);

It is very normal example of a function call. The compute function is called when the line i=this.compute(10) is called.  We can replace the above code with inline annonymous function like this:

delegate int DelType(int i);
DelType dd =  delegate(int value)
               return (value +2);
int i = dd(10);

In the 2nd Case we can see that just declaring a Delegate and giving instance of that delegate type we can easily manage to write annonymous methods. .NET 3.5 puts forward this concept a little more compact. We can take the use of => operator which is introduced in .NET 3.5. It gives more flexible and easier way to write expression. Lambda Expressions can be used in the prevous case to get the result like this:

delegate int DelType(int i);
DelType d = value => value + 2;
int i = d(10);  

Thus the delegate type is assigned directly. The meaning of the line value =>value + 2 is just similar to declaring a function.  The first value indicates the arguments that we pass in a function and the one after the "=>" operator is the body of the function. Similarly if we want we can feed in as many arguments as we want to. Just in the following examples :

// Method with 2 arguments
delegate int DelType(int i, int j);
DelType d = (value1,value2) => value1 + value2;
int i = d(10,20) // Returns 30

In this example we passed in 2 arguments to the function and returns the result.  You may also wonder how can I write a lambda expression when your function body takes more than one expression. Dont worry, its simple. Take a look at the following example.

// Multiline Function Body
delegate int DelType(int i, int j);
DelType d = (value1,value2) => {
           value1 = value1 + 2;
           value2 = value2 + 2;
           return value1 + value2;

Thus we see writing Lambda expression is very easy. This comes very handy when working with linq, as most of the extension methods that are associated with Linq takes function delegates as arguments. We can pass simple delegate function body easily to those functions to perform work easily.

These type of expressions are also called as Expression Trees as the basic unit of this expressions are data and structured in tree format. We can also create dynamic Expression types ourself if we like to using the Expression Class.

Expression<Func<int, int>> exprTree = num => num * 5;

// Decompose the expression tree.
ParameterExpression param = (ParameterExpression)exprTree.Parameters[0];
BinaryExpression operation = (BinaryExpression)exprTree.Body;
ParameterExpression left = (ParameterExpression)operation.Left;
ConstantExpression right = (ConstantExpression)operation.Right;

Thus if we write a function which receives the ExpressionTrees we can decompose the parameters easily.

VB.NET doesnt support annonymous methods and lambda expressions with statement body now.

Extension Methods 

Another new concept that comes with .NET 3.5 is the Extension methods. Now we can include our own custom methods in already defined objects. We can create static classes and include custom methods to objects.  Extension method behavior is similar to that of static methods. You can declare them only in static classes. To declare an extension method, you specify the keyword this as the first parameter of the method.  Let us look at the following example:

public static class ExtensionMethods
   public static int ToInt32Extension(this string s)
      return Int32.Parse(s);

If we include the namespace to our application, any string variable will have ToInt32Extension method. This function takes 0 arguments and passes the string to s. We can also pass parameters just like below:

public static class ExtensionMethods
   public static int ToInt32ExtensionAddInteger(this string s,int value)
      return Int32.Parse(s) + value;

Here integer value is also passed. 

We can also create ExtensionMethod that takes function as argument. The class Enumerable extends a number of methods which makes use of Func class to send a method delegate within the extension so that it could work with the IEnumerable list of objects. Let us create our own extension method that can work with IEnumerable as well, just look at the code below:

public static int GetMinVal<TSource>(this IEnumerable<TSource> source,
                                    Func<TSource, int> selector)
     int smallest = int.MaxValue;
     foreach (var item in source)
          int nextItem = selector(item);
          if (nextItem < smallest)
              smallest = nextItem;
     return smallest;
// calling the Function
int minimum = employees.GetMinVal<Employee>(emp => emp.age);

Now if I call this function, it will return the Min value. The selector function will be the annonymous delegate passed in. Thus the selector will call the function like

public int selector(Employee emp)
    return emp.age;

Thus it will compare all the ages of employees, and gets the minimum of them.

Similar to this, let us write Where function ourself:

public static IEnumerable<TSource> MyOwnWhere<TSource>(this IEnumerable<TSource> source,
                       Func<TSource, bool> predicate) where TSource : class
           foreach (var item in source)
               if (predicate(item))
                   yield return item;


This will call the function predicate that we pass into the function, and if the function returns true, it will grant it. Thus we make a with return of a list of source similar to Where clause as that is defined in Enumerable class as extension method.

What is Yield ? 

You may notice here I am using yield keyword to generate the list of items. This is one of the new addition to C# 2.0. The meaning of this keyword is to instruct the program to run and generate the list until all the loop execution is exhausted. Thus it creates a list of items for which the predicate function returns true.

FOR VB.NET developers 
In case of VB.NET we have to define the extension method inside a Module with Attribute

Module ExtensionModule
    <System.Runtime.CompilerServices.Extension()> _
    Public Function ToInt32ExtensionAddInteger(ByVal s As String, _
                            ByVal value As Integer) As Integer
        Return Int32.Parse(s) + value
    End Function
End Module

Annonymous Type 

Annonymous types introduced with .NET can create object without even declaring the classes. The members of the annonymous types are implicitely defined during compilation

var x = new { Name ="AA", Age =30, ... };

This would create  a new object of annonymous type.


Meaning of VAR 

Var is a new keyword added in .NET 3.5. This is called Implicit Type Variable. During runtime it automatically assigns its type of the variable based on the object if finds.

var x = 10 // implies the variable is of integer type
var x = new list<Employee>(); // Means x  holds list of employees

var is very useful when working with annonymous typed just introduced. It assigns the type of the variable based on the type of the object passed in, so there is no difference of doing List<Employee>() x = new List<Employee>() and doing var x = new List<Employee>() as in both the case the type of the variable x is always get to List<Employee>.

Only restriction is we cannot define members of var type.

Demonstration of Linq

Let us now demonstrate LINQ with a simple example. The example can be found on the source code associated with the article.
First we define

    List<Employee> employees = new List<Employee>();
    List<Employee> employee1 = new List<Employee>();
    List<Order> orders = new List<Order>();
    //Where Employee and Order are classes.
    public class Employee
        public string name;
        public int age;
        public override string ToString()
    public class Order
        public string itemName;
        public string empName;
        public override string ToString()
            return this.itemName;


Private employees As New List(Of Employee)()
Private employee1 As New List(Of Employee)()
Private orders As New List(Of Order)()
//Where Employee and Order are classes.
Public Class Employee
    Public name As String
    Public age As Integer
    Public Overloads Overrides Function ToString() As String
    End Function
End Class
Public Class Order
    Public itemName As String
    Public empName As String
    Public Overloads Overrides Function ToString() As String
        Return Me.itemName
    End Function
End Class

We would make use of these three lists in our examples.


While writing queries, you will find a number of operators that we can use. They most general opreators are :
  • Projection operators  (Select)
  • Restriction operators (Where)  
  • Partitioning operators (Take / Skip, TakeWhile/ SkipWhile)
  • Join operators  (Join)
  • Concatenation operator (Concat)
  • Ordering operators  (order by)
  • Grouping operators  (group by into)

Projection Operator 

With the word projection, I mean Select statements. Every linq elements should have projection in it.

var iNames = from i in employees 

//Using Lambda Expression
var iNames = employees.Select<Employee, string>(r =>;

Dim iNames = From i In employees _

Here IEnumerable of names of employees is returned. In the above example we are also taking advantage of annonymous type declaration that was introduced in .NET 3.5.

Restriction Operator 

Restriction operator can be applied by using Where clause. Example of Where clause:

var filterEnumerable = from emp in employees
                       where emp.age > 50
                       select emp;

 //Using Lambda Expression 
var filterEnumerable = employees.Where<Employee>(emp => emp.age > 50); 
Dim filterEnumerable = From emp In employees _
    Where emp.age > 50 _
    Select emp

This filters out Employees by age greater than 50.

Partitioning using Take /Skip operators 

Take can be used when we take first N elements in a list, skip will take the elements after N. 

var filterEnumerable = (from emp in employees
                         select emp).Take<Employee>(2);

 //Using Lambda Expression                                
var filterEnumerable = employees.Select<Employee, string>(r =>;
Dim filterEnumerable = (From emp In employees _
    Select emp).Take(Of Employee)(2)

Takewhile and skipwhile operator will select from a list based on a delegate passed in.
var filterEnumerable = (from emp in employees 
                        select emp).SkipWhile<Employee>(
                        r => > 4);

 //Using Lambda Expression 
var filterEnumerable = employees.Select<Employee, Employee>(
r => { 
       return r; 
     }).SkipWhile<Employee>(r => > 4); 
Dim filterEnumerable = (From emp In employees _
    Select emp).SkipWhile(Of Employee)(Function(r) > 4)

Join Operators: 

Join operators have 2 parts. The outer part gets results from inner part and vice versa so returns the result based on both
var filterEnumerable = from emp in employees
                       join ord in orders
                       equals ord.empName 
                       select emp;

 //Using Lambda Expression 
var filterEnumerable = employees.Join<Employee, Order, string, Employee>
                       (orders, e1 =>, o => o.empName, (o, e2) => o);

Dim filterEnumerable = From emp In employees _
                      Join ord In orders On = ord.empName _
                      Select emp
Here we are joining employees and order based on the names passed in. 

Concatenation Operator : 

The Concatenation operator concats two sequence.
var filterEnumerable = (from emp in employees
                        select emp).Concat<Employee>(
                        from emp in employees1
                        select emp);

 //Using Lambda Expression 
var filterEnumerable = employees.Concat<Employee>(

Dim filterEnumerable = (From emp In employees _
    Select emp).Concat(Of Employee)(From emp In employees1 _
    Select emp)
This will concat categories of Entertainment and Food. Distinct oprator can also be used to evaluate only distinct elements in resultset. 

OrderBy / ThenBy     

Orderby/ThenBy can be used to order dataresults. 
var orderItems = from emp in employees 
                 orderby, emp.age descending;

  //Using Lambda Expression  
var orderItems =employees.OrderBy(i => => i.age);
Dim filterEnumerable = From emp In employees Order By, emp.age  Descending

Here the ordering is done by name and then decending by age. 

GroupBy Operator :   

This is used to group elements.
var itemNamesByCategory = from i in _itemList
                          group i by i.Category into g
                          select new { Category = g.Key, Items = g };
Dim itemNamesByCategory = From i In _itemList _
    Group i By i.Category _
    Into g _
        Select g.Key


This gets all the categories and items grouped by category. Well this grouping seems to be a little tricky for me. Let me make you understand what exactly is the way. Here while we are grouping, we are taking the group into g(which is a IGrouping<tsource,telement>). The g will have a key, which holds the grouped data, you can add multiple grouping statement. If you want to have a having clause, its just the where clause will do. Just like the example below:

var filterEnumerable2 = from emp in employees 
                        where emp.age >65 //Normal Where clause works on all items 
                        group emp by emp.age into gr where gr.Key > 40 
                        select new { 
                                     aaa = gr.Key, 
                                     bbb = gr 

Here in the example, I have returned the aggregate functions like sum, count etc which you can find from group data.

Distinct / Union / Intersect / Except Operators : 

Union operator produces an union of two sequences
var un = (from i in _itemList
          select i.ItemName).Distinct()
          .Union((from o in _orderList
          select o.OrderName).Distinct()); 
Intersect operator produces an intersection of two sequences.
var inter = (from i in _itemList
             select i.ItemID).Distinct()
             .Intersect((from o in _orderList
             select o.OrderID).Distinct());
Dim inter = (From i In _itemList _
    Select i.ItemID).Distinct().Intersect((From o In _orderList _
    Select o.OrderID).Distinct())

Except operator produces a set of difference elements from two sequences.

var inter = (from i in _itemList
             select i.ItemID).Distinct()
             .Except((from o in _orderList
             select o.OrderID).Distinct())

Use of let    

We can create temporary variables within LINQ statements using let keyword

var filterEnumerable = from emp in employees
                       let x = emp.age
                       let y = x * 5
                       select x;
Dim filterEnumerable = From emp In employees _
    Let x = emp.age _
    Let y = x * 5 _
    Select x


Here x and y are intermediate variables and we will get enumerable of integers 5 times the age of each employees.


DLINQ or LINQ to SQL provides a runtime framework to work with Relation Database objects. It translates LINQ queries into SQL queries automatically. We can create the total snapshot of the database using LINQ objects so that the entire relationship can be created in application classes and objects. There are tools that can create those objects automatically, we can make use of them in our program. We can make use of DataContext classes to get the flavour of DLINQ. .NET makes use of enhanced Attributes to define classes.

Let us take an example 

  public class Employee
  public string EmpID;
  public string Name;

This means Employee class corresponds to the actual Employee table. Where EmpID is the primary key of the table and Name is another column.

For Further Reading:


Similar to DLINQ, XLINQ means using LINQ to XML documents. .NET extended the little jem LINQ to create a number of classes which can manipulate XML documents very easily. Compared to XML DOM, XLINQ is much more flexible because it does not require you to always have a document object to be able to work with XML. Therefore, you can work directly with nodes and modify them as content of the document without having to start from a root XmlDocument object. This is a very powerful and flexible feature that you can use to compose larger trees and XML documents from tree fragments. Let us look at the following example:  

XDocument bookStoreXml =
      new XDocument(
       new XDeclaration("1.0", "utf-8", "yes"),
       new XComment("Bookstore XML Example"),                  
       new XElement("bookstore", new XElement("genre",
         new XAttribute("name", "Fiction"),
         new XElement("book", new XAttribute("ISBN", 
           "10-861003-324"), new XAttribute("Title", "A Tale of Two Cities"),
          new XAttribute("Price", "19.99"),                             
          new XElement("chapter", "Abstract...", new XAttribute("num", "1"),
           new XAttribute("name", "Introduction")),                        
          new XElement("chapter", "Abstract...", new XAttribute("num", "2"),
           new XAttribute("name", "Body")  

In this example, we have created an XML document using XDocument class. The XDocument class may contain XElements which are the XML elements.XDeclaration is the class to create XML declaration. XAttribute creates an XML attributes. XComment creates XML comments. In this example Root element bookstore holds all the child elements inside it. We can  pass an entire tree of XElement inside any XElement thus it gives easier way to create XML documents.

After reading, creating or manipulating the xml document, we can save the document easily to the disk using save method on XDocument object.


It will save the entire document to the disk.

To read the document again and create the entire tree structure of object in application end we call the load method of XElement.


We can also use parse method if we want to load the xml from a string.

You can query XML document in the same way as we use in objects. We can make use of some methods Ancestors (which returns all the ancestor elements in IEnumerable list), Descendants (returns all the decendants of the element), Element(returns the child element with name), Elements(returns all the child elements of the current node), Nodes (returns the content of the current node)

For Further Reading


For Further reading, you may look into the following links:
1. Charlie Claverts Blog
2. Msdn Article on Linq Expression
3. LINQ Article on MSDN


Therefore using Linq we can easily handle search among complex application data easily leverage out lots of loops and conditional statements.


This is the first version as on 1st March 2009.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Abhishek Sur
India India
Did you like his post?

Oh, lets go a bit further to know him better.
Visit his Website : to know more about Abhishek.

Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.

Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.

Working as a VP product of APPSeCONNECT, an integration platform of future, he does all sort of innovation around the product.

Have any problem? Write to him in his Forum.

You can also mail him directly to

Want a Coder like him for your project?
Drop him a mail to

Visit His Blog

Dotnet Tricks and Tips

Dont forget to vote or share your comments about his Writing

Comments and Discussions

QuestionVar Pin
DeependraSingh201213-Nov-16 22:26
MemberDeependraSingh201213-Nov-16 22:26 
PraiseThanks for Awesome Article Pin
Rahul Nehare7-Mar-16 21:41
professionalRahul Nehare7-Mar-16 21:41 
Question"var" works at compile time Pin
Darpanw20-May-15 22:20
MemberDarpanw20-May-15 22:20 
AnswerRe: "var" works at compile time Pin
Abhishek Sur20-May-15 22:51
professionalAbhishek Sur20-May-15 22:51 
QuestionLinq and Lambda Expression best example Pin
amarjeet001112-Apr-14 19:38
Memberamarjeet001112-Apr-14 19:38 
QuestionMy Vote of 5* Pin
Mas118-Mar-14 22:25
MemberMas118-Mar-14 22:25 
QuestionBeautiful Pin
Saqib Mobeen7-Nov-13 18:53
MemberSaqib Mobeen7-Nov-13 18:53 
SuggestionSpelling Mistake Pin
joshrduncan201222-Oct-13 9:26
Memberjoshrduncan201222-Oct-13 9:26 
QuestionMy vote of 4 Pin
Janilane11-Oct-13 20:00
MemberJanilane11-Oct-13 20:00 
GeneralMy vote of 5 Pin
K K Kodoth27-Aug-13 2:59
MemberK K Kodoth27-Aug-13 2:59 
GeneralMy vote of 3 Pin
chaithu64812-Jul-13 0:32
Memberchaithu64812-Jul-13 0:32 
GeneralMy vote of 5 Pin
Sandesh M Patil10-Jul-13 8:24
MemberSandesh M Patil10-Jul-13 8:24 
GeneralMy vote of 5 Pin
Mohammed Hameed13-May-13 22:40
professionalMohammed Hameed13-May-13 22:40 
QuestionWrong in Meaning of Var Pin
yougetvara11-Apr-13 22:24
Memberyougetvara11-Apr-13 22:24 
GeneralMy vote of 5 Pin
Hasibul Haque11-Apr-13 11:06
professionalHasibul Haque11-Apr-13 11:06 
QuestionSimple and Super Pin
Member 99571521-Apr-13 4:08
MemberMember 99571521-Apr-13 4:08 
QuestionWrong line in Partitioning using Take /Skip operators Pin
_Noctis_17-Feb-13 18:13
professional_Noctis_17-Feb-13 18:13 
GeneralVery helpful Pin
RajeevGopal6-Feb-13 6:21
MemberRajeevGopal6-Feb-13 6:21 
GeneralMy vote of 4 Pin
Hitesh R28-Jan-13 17:49
MemberHitesh R28-Jan-13 17:49 
GeneralMy vote of 5 Pin
tyagiradhika41-Nov-12 4:12
Membertyagiradhika41-Nov-12 4:12 
GeneralMy vote of 5 Pin
cblessinger30-Aug-12 4:57
Membercblessinger30-Aug-12 4:57 
GeneralMy vote of 3 Pin
Mehul M Thakkar3-Aug-12 1:03
MemberMehul M Thakkar3-Aug-12 1:03 
GeneralMy vote of 4 Pin
Renju Vinod17-Jul-12 21:15
professionalRenju Vinod17-Jul-12 21:15 
QuestionVB.NET Example? Pin
Daniel Kamisnki12-Jul-12 9:24
MemberDaniel Kamisnki12-Jul-12 9:24 
GeneralMy vote of 5 Pin
peetinun4-Jul-12 22:56
Memberpeetinun4-Jul-12 22:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.