Basics of LINQ & Lamda Expressions - CodeProject


13,428,563 members (43,560 online)



quick answers

Sign in





Search for articles, questions, tips

Articles » Platforms, Frameworks & Libraries » .NET Framework » General

Basics of LINQ & Lamda Expressions

Article Browse Code

Abhishek Sur, 28 Feb 2009


4.80 (133 votes)

Revisions (15) Alternatives

Rate this:

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)

Comments (59) Add your own alternative version

Download - 58.65 KB

Tagged as

1. 2. 3. 4. 5. 6. 7. 8. 9. 10.

C#3.0 VB9.0 .NET3.5 C# VB .NET

Introduction What is LINQ? Types of LINQ. Lambda Expression Extension Methods What is Yield? Annonymous Types Meaning of VAR Demonstration of Linq (With Example) Operators :


Projection Operator Restriction Operator Partition Operator Join Operator Concatenation Operator Ordering Operator Grouping Operator Distinct / Union/ Intersect / Except Operator Use of Let within Linq

Beginner Stats 679.2K views 3.9K downloads 262 bookmarked 11. 12. 13. 14. 15.

Posted 28 Feb 2009 CPOL

DLINQ (LINQ TO SQL) XLINQ (LINQ TO XML) Reference Conclusion History

Introduction 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 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. Hide Copy Code

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: Hide Copy Code

//C# 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: Hide Copy Code

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 : Hide Copy Code

// 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. Hide Copy Code

// 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. Hide Copy Code

Expression> 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. NOTE: 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: Hide Copy Code

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: Hide Copy Code

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: Hide Copy Code

public static int GetMinVal(this IEnumerable source, Func 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(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 Hide Copy Code

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: Hide Copy Code

public static IEnumerable MyOwnWhere(this IEnumerable source, Func 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 . Hide Copy Code

Module ExtensionModule _ 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 Hide Copy Code

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. Hide Copy Code

var x = 10 // implies the variable is of integer type var x = new list(); // 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() x = new List() and doing var x = new List() as in both the case the type of the variable x is always get to List. 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 Hide Shrink

Copy Code

C# List employees = new List(); List employee1 = new List(); List orders = new List(); //Where Employee and Order are classes. public class Employee { public string name; public int age; public override string ToString() { return; } } public class Order { public string itemName; public string empName; public override string ToString() { return this.itemName; } } VB.NET 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 Return 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.

Operators 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. C# var iNames = from i in employees select; //Using Lambda Expression var iNames = employees.Select(r =>; Hide Copy Code

VB.NET Dim iNames = From i In employees _ Select 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(emp => emp.age > 50); Hide Copy Code

Vb.NET 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(2); //Using Lambda Expression var filterEnumerable = employees.Select(r =>; Hide Copy Code

VB.NET 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. Hide Copy Code

var filterEnumerable = (from emp in employees select emp).SkipWhile( r => > 4); //Using Lambda Expression var filterEnumerable = employees.Select( r => { return r; }).SkipWhile(r => > 4); Hide Copy Code

VB.NET 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 on equals ord.empName select emp; //Using Lambda Expression var filterEnumerable = employees.Join (orders, e1 =>, o => o.empName, (o, e2) => o); Hide Copy Code

VB.NET 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( from emp in employees1 select emp); //Using Lambda Expression var filterEnumerable = employees.Concat( employees1.AsEnumerable()); Hide Copy Code

VB.NET 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. Hide Copy Code

var orderItems = from emp in employees orderby, emp.age descending; //Using Lambda Expression var orderItems =employees.OrderBy(i => => i.age); Hide Copy Code

VB.NET 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 }; Hide Copy Code

VB.NET 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). 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: Hide Copy Code

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, ccc=gr.Count(), ddd=gr.Sum(r=>r.age), 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 Hide Copy Code

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. Hide Copy Code

var inter = (from i in _itemList select i.ItemID).Distinct() .Intersect((from o in _orderList select o.OrderID).Distinct()); Hide Copy Code

VB.NET 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. Hide Copy Code

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 Hide Copy Code

var filterEnumerable = from emp in employees let x = emp.age let y = x * 5 select x; Hide Copy Code

VB.NET 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 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 Hide Copy Code

[Table(Name="Employee")] public class Employee { [Column(Id=true)] public string EmpID; [Column] 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: LINQ to SQL on MSDN

XLINQ 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: Hide Copy Code

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.

bookStoreXml.Save(@"C:\XMLDocuments\Bookstore.xml"); 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.

XElement.Load(@"C:\XMLDocuments\Bookstore.xml"); 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 XLINQ on MSDN

Reference 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

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

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

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



About the Author Abhishek Sur Architect


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. Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well. Have any problem? Write to him in his Forum. You can also mail him directly to [email protected] Want a Coder like him for your project? Drop him a mail to [email protected] Visit His Blog Dotnet Tricks and Tips

Dont forget to vote or share your comments about his Writing

You may also be interested in... Expression Tree Basics

Window Tabs (WndTabs) Add-In for DevStudio

Databinder.Eval using Lamda Expressions

Introduction to D3DImage

SAPrefs - Netscape-like Preferences Dialog

Creating alternate GUI using Vector Art

Comments and Discussions

You must Sign In to use this message board.

Search Comments

Spacing Relaxed

Layout Normal

Per page 25


First Prev Next



Thanks for Awesome Article


"var" works at compile time


20-May-15 22:20

Abhishek Sur

20-May-15 22:51

Linq and Lambda Expression best example


12-Apr-14 19:38

My Vote of 5*


8-Mar-14 22:25


Saqib Mobeen

7-Nov-13 18:53

Spelling Mistake


22-Oct-13 9:26

My vote of 4


My vote of 5

K K Kodoth

27-Aug-13 2:59

My vote of 3


12-Jul-13 0:32

My vote of 5

$andesh M Patil

10-Jul-13 8:24

My vote of 5

Mohammed Hameed

13-May-13 22:40

Wrong in Meaning of Var


11-Apr-13 22:24

My vote of 5

Hasibul Haque

11-Apr-13 11:06

Simple and Super

Member 9957152

Wrong line in Partitioning using Take /Skip operators


Very helpful


My vote of 4


My vote of 5


My vote of 5


My vote of 3

Mehul M Thakkar

My vote of 4


VB.NET Example?

Daniel Kamisnki

12-Jul-12 9:24

My vote of 5


4-Jul-12 22:56

Re: "var" works at compile time

Last Visit: 31-Dec-99 19:00 Last Update: 7-Mar-18 13:22 General




7-Mar-16 21:41

11-Oct-13 20:00

1-Apr-13 4:08 17-Feb-13 18:13 6-Feb-13 6:21 28-Jan-13 17:49 1-Nov-12 4:12 30-Aug-12 4:57 3-Aug-12 1:03 17-Jul-12 21:15

1 2 3 Next »

Refresh Bug



13-Nov-16 22:26




Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages. Permalink | Advertise | Privacy | Terms of Use | Mobile Web02-2016 | 2.8.180306.2 | Last Updated 1 Mar 2009

Layout: fixed | fluid

Article Copyright 2009 by Abhishek Sur Everything else Copyright © CodeProject, 1999-2018


Basics of LINQ & Lamda Expressions - CodeProject

13,428,563 members (43,560 online) home articles quick answers Sign in discussions features community help Search for articles, questions, t...

354KB Sizes 2 Downloads 5 Views

Recommend Documents

LINQ - Lambda Expressions - TutorialsPoint
However, it is not possible to execute a lambda expression in this form. Example of a lambda expression in C# is shown b

C# - CodeProject
C# - Free source code and tutorials for Software developers and Architects.; Updated: 9 Dec 2017.

classes - CodeProject
Oct 16, 2015 - In this post, we will be covering the very basics of how to create properties in our Scala classes, and a

C# - CodeProject
C# - Free source code and tutorials for Software developers and Architects.; Updated: 5 Dec 2017.

ExcelFormat Library - CodeProject
#define XLS_FORMAT_GENERAL L"General" #define XLS_FORMAT_TEXT L"@" #define XLS_FORMAT_INTEGER L"0" #define XLS_FORMAT_DE

MuleSoft Variables Exemplified - CodeProject
Feb 7, 2017 - After setting a flow variable programmers are able to access them through the following "Mule Expression L

C++ Language - CodeProject
C / C++ Language - Free source code and tutorials for Software developers and Architects.; Updated: 7 Dec 2017.

Password hacker - CodeProject
A simple BHO to retrieve the user ID and password.; Author: ram verma; Updated: 17 Sep 2006; Section: ATL; Chapter: Plat

Romeo and Juliet - CodeProject
Dec 3, 2011 - Using the XTree application I recently wrote about, we're going to define the various entities and their r

Hotel Reservation System - CodeProject
Mar 28, 2009 - An explanation of the useful code within Hotel Reservation System; Author: Jomar Pabuaya; Updated: 30 Mar