LINQ to Object


What is LINQ?

LINQ stand for Language-Integrated Query is now available as a integral part of Visual Studio 2008.

LINQ has a great power of querying on any source of data, data source could be the collections of objects, database or XML files. 

We can easily retrieve data from any object that implements the IEnumerable<T> interface. 

Microsoft basically divides LINQ into three areas and that are give below.
LINQ to Object
LINQ to ADO.Net  
  • LINQ to SQL
  • LINQ to Dataset
  • LINQ to Entities

What is LINQ?
LINQ stands for Language integrated Query, that allows us to query local object collection or remote data source.

Using LINQ we can query any collection implementing IEnumaerable interface. To use LINQ in code we need to use Sysrtem.Linq namespace.
string[] names = { "Ajay", "Vijay", "Karan", "Farhan", "Pooja", "Geeta" };
IEnumerable tp = names.Where(n => n.EndsWith("a"));
foreach (string str in tp)

Here we have taken a string array in first line of code, fnames that contains 6 names. 
In second line of code we write a Linq to get the names ending with "a".


Main Points:
1. Linq data has two parts, sequence and elements. Here names is a sequence and array members are elements.
2. Linq doesn't alter the input sequence. So result will always be in the order it was input.
3. Linq always return an IEnumerable result.
4. "n => n.EndsWith("a")" is a lambda expression that filters the result for us.

Here we will start using some advance operators and will write code as lambda expressions as well as comprehension query.

string[] names = { "Ajay", "Vijay", "Karan", "Farhan", "Pooja", "Geeta", "Dia" };

Lambda Expression:

IEnumerable tp = names.Where(n => n.EndsWith("a"))
                            .OrderBy(n=> n.Length)
                            .Select(n => n.ToUpper());

Comprenesion Query:

IEnumerable tp = from n in names
                         where n.EndsWith("a")
                         orderby n.Length
                         select n.ToUpper();    

Enumerate result:

foreach (string str in tp)





Main Points:
1. We have option to use either lambda expressions or comprehension query to get the desired result.
2. Compiler converts comprehension query to lambda expression.

Let us see an example first. Suppose there is a list of integers like this:
List list = new List() { 1, 2, 3, 4, 5, 6, 100 };

To find all the even numbers in this list, you might write code like this:

List list1 = new List();

foreach (var num in list)


    if (num % 2 == 0)



Now with LINQ, you can select all of the even numbers from this list
and assign the Query result to a variable, in just one sequence. like this:
var list2 = from number in list

            where number % 2 == 0

            select number;

In this example, list2 and list1 are equivalent. list2 contains the same numbers as 
list1 does. As you can see, you don't write a foreach loop. Instead, you write a SQL 


Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.

Restriction Operators

Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In suscipit faucibus urna.


Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis. Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.

  • List item one
  • List item two
  • List item three


Cras dictum. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia mauris vel est.

Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

Aggregate Operators


Partitioning Operators

Element Operators


Miscellaneous Operators