Linq Tutorial

Linq Tutorial

Introduction:

Language Integrated Query (LINQ) is a language feature in the .NET Framework that enables you to use common syntax to query data in a collection, an XML document, a database, or any type that supports the IEnumerable<T> or IQueryable<T> interface.

Why Need Linq:

Prior to LINQ, a developer needed to learn different syntax depending on the source of the data. If the source were a database, you needed to learn SQL. If the source were an XML document, you needed to learn XQuery. If the source were an array or a collection, you would write a looping structure, such as a foreach loop, that would enumerate through the items in the collection and filter them appropriately. LINQ enables you to use common syntax regardless of what the source is.

Linq provides two different styles of syntax for linq Query, expressions-based are the first style, and method-based queries are the second.

Select

Select method returns all element from the sequence.

// Retrieving all elements from the list

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Expression based query

var data = from x in list

            select x;

// Method based query

var data1 = list.Select(x => x);

 

foreach (int num in data)

{

    Response.Write(num);

}

// Output :- 0 1 2 3 4 5 6 7 8 9 10

Where

Where method use to retrieve elements from the sequence according to the condition.

// Retrieving elements from the list which are greater than 5

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

var data = from x in list

            where x > 5

            select x;

// Method based query

var data1 = list.Where(x => x > 5);

 

foreach (int num in data)

{

    Response.Write(num);

}

// Output:- 6 7 8 9 10

Any

Any method use to check any element is present or not according to condition from the sequence.

// Check any element present in the sequence which is divided by 5

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

var data = (from x in list

            select x).Any(x => x % 5 == 0);

// Method based query

var data1 = list.Any(x => x % 5 == 0);

 

// Output:- True

All

All method use to check all elements from the sequence satisfy a particular condition or not.

// Check all element present in the sequence which is divided by 2

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Expression based query

var data = (from x in list

            select x).All(x => x % 2 == 0);

// Method based query

var data1 = list.All(x => x % 2 == 0);

 

// Output:- False

Contains

Contains method use to check a partucular element from the sequence is present or not.

// Check element 5 is present in the sequence or not

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Expression based query

var data = (from x in list

            select x).Contains(5);

// Method based query

var data1 = list.Contains(5);

 

// Output:- True

First

First method returns first element from sequence.

// Retrieve first element from list

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

int first = (from x in list

               select x).First();

 

// Method based query

int first1 = list.First();

 

// Output:- 1

FirstOrDefault

FirstOrDefault method returns first element from the sequence if element exists according to condition otherwise it returns default value.

// Retrieve first element from list which is greater that 10

// If element not exists then return default value

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

int num = (from x in list

           where x > 10

           select x).FirstOrDefault();

 

// Method based query

int num1 = list.FirstOrDefault(x => x > 10);

 

// Output:- 0

 

Last

Last method returns last element from the sequence.

// Retrieve last element from  the list

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

int last1 = (from x in list                  

            select x).Last();

 

// Method based query

int last1 = list.Last();

 

// Output:- 10

LastOrDefault

LastOrDefault method returns last element from the sequence if element exists according to condition otherwise it returns default value.

// Retrieve last element from the list which is greater than 10
// If not exists then returns default value

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

    int num = (from x in list

               where x > 10

               select x).LastOrDefault();

 

// Method based query

int num1 = list.LastOrDefault(x => x > 10);

 

// Output:- 0

Single

Single method returns a single element from the sequence.

// Retrieve a single element 10 from the list

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

int num = (from x in list

           where x == 10

           select x).Single();

 

// Method based query

int num1 = list.Single(x => x == 10);

 

// Output:- 10

SingleOrDefault

SingleOrDefault method returns a single element from the sequence if element exists according to condition otherwise it returns default value.

// Retrieve fa sigle element 11 from the list
// If not exists then returns default value

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

int num = (from x in list

           where x == 11

           select x).SingleOrDefault();

 

// Method based query

int num1 = list.SingleOrDefault(x => x == 11);

 

// Output:- 0

ElementAt

ElementAt method returns an element from the squence at specific position.

// Retrieve an element from the list which is at 5th index

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

int num = (from x in list

           select x).ElementAt(5);

 

// Method based query

int num1 = list.ElementAt(5);

 

// Output:- 6

ElementAtOrDefault

ElementAtOrDefault method returns an element from sequence at specific position if element exists according to condition otherwise it returns default value.

// Retrieve an element from the list which is at 11th index

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

int num = (from x in list

           select x).ElementAtOrDefault(11);

 

// Method based query

int num1 = list.ElementAtOrDefault(11);

 

// Output:- 0

Take

Take method returns number of records whatever specified in Take method.

// Retrieve top 5 elements

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

var data = (from x in list

            select x).Take(5);

 

// Method based query

var data1 = list.Take(5);

 

foreach (int num in data)

{

   Response.Write(num);

}

// Output:- 1 2 3 4 5

TakeWhile

TakeWhile method returns records upto contidion is satisfied in the squence.

// Retrieve top 5 elements which are less than 5

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

var data = (from x in list

            select x).TakeWhile(x => x < 5);

 

// Method based query

var data1 = list.TakeWhile(x => x < 5);

   

foreach (int num in data)

{

    Response.Write(num);

 

}

// Output:- 1 2 3 4

Skip

Skip method skip number records whatever you specified in Skip method and returns remaining records.

// Skip top 5 elements

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

var data = (from x in list

            select x).Skip(5);

 

// Method based query

var data1 = list.Skip(5);

 

foreach (int num in data)

{

    Response.Write(num);

}

// Output:- 6 7 8 9 10

SkipWhile

SkipWhile method skiping records according to condition and returns remaining records from the sequence.

// Skip elements which are less than 5

int[] list = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 

// Expression based query

var data = (from x in list

            select x).SkipWhile(x => x < 5);

 

// Method based query

var data1 = list.SkipWhile(x => x < 5);

   

foreach (int num in data)

{

    Response.Write(num);

}

// Output:- 5 6 7 8 9 10

SequenceEqual

SequenceEqual method true if first sequnce exactly macthing with second sequence otherwise it returns false.

// Checking both sequence is equal or not

int[] list1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int[] list2 = new int[] { 8, 9, 10, 11, 12, 13, 14, 15 };

 

// Expression based query

bool IsEqual = (from x in list1

                select x).SequenceEqual(list2);

 

// Method based query

bool IsEqual1 = list1.SequenceEqual(list2);

 

// Output: - false

Except

Except method returns all records from first sequence which are not present in second sequence.

// Retrieve elements form the first list which are not present in second list

int[] list1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int[] list2 = new int[] { 8, 9, 10, 11, 12, 13, 14, 15 };

 

// Expression based query

var difference = (from x in list1

                  select x).Except(list2);

 

// Method based query

var difference1 = list1.Except(list2);

 

foreach (int num in difference)

{

    Response.Write(num);

}

// Output: - 1 2 3 4 5 6 7

Intersect

Intersect method returns all records from all sequence which are present in every sequence.

// Retrieve elements which are prsent in both sequences

int[] list1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

 int[] list2 = new int[] { 8, 9, 10, 11, 12, 13, 14, 15 };

 

// Expression based query

var common = (from x in list1

              select x).Intersect(list2);

 

// Method based query

var common1 = list1.Intersect(list2);

 

foreach (int num in common)

{

    Response.Write(num);

}

// Output:- 8 9 10

Concat

Concat method use to add two different sequence into a new seuence of same type.

// Combined two sequence into one sequence

int[] list1 = new int[] { 1, 2, 3, 4, 5 };

int[] list2 = new int[] { 11, 12, 13, 14, 15 };

 

// Expression based query

var concatList = (from x in list1

                select x).Concat(list2);

 

// Method based query

var concatList1 = list1.Intersect(list2);

 

foreach (int num in concatList)

{

    Response.Write(num);

}

// Output:- 1 2 3 4 5 11 12 13 14 15

Distinct

Distinct method returns only unique elements from the sequence.

// Retrieve only distinct elements from the list

int[] list = new int[] { 1, 3, 5, 4, 5, 1, 6 };

 

// Expression based query

var distinct = (from x in list

                    select x).Distinct();

 

// Method based query

var distinct1 = list.Distinct();

 

foreach (int num in distinct)

{

    Response.Write(num);

}

 

// Output:- 1 3 5 4 6