We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Saturday, May 2, 2015

What is LINQ with practical examples


What is LINQ?
LINQ is the acronym for Language Integrated Query. LINQ is a Microsoft technology to perform operations on nearly all data sources. Here I have described nearly all data sources. Yes it supports General Arrays, Generic Lists, XML, Databases and even flat files. The beauty of LINQ is uniformity. Now, how does it provide uniformity?

Process integer array with LINQ.

The following code displays all items of an Array:


static void Main(string[] args)

{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = from n in numbers
select n;
Console.WriteLine("Print all number");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
Console.ReadLine();
}


If we want to display only even numbers then use a where clause in the LINQ query:

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = (from n in numbers
where(n % 2 == 0)
select n);
Console.WriteLine("Select even number only");
Console.WriteLine(lowNums);
Console.ReadLine();
}

This display even number

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = (from n in numbers
where(n % 2 != 0)
select n);
Console.WriteLine("Select even number only");
Console.WriteLine(lowNums);
Console.ReadLine();
}

Sometimes its necessary to sort an item by a sorting condition, here the code will Print all items where the item is greater than 5:

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = from n in numbers
where n>5
select n; 

Console.WriteLine("Print all item where item is greater than 5");

foreach (var x in lowNums)
{
Console.WriteLine(x);
}
Console.ReadLine();
}

We can specify an "or" ("||") operation in our LINQ query like in the code below. The code below will print all items where the item is greater than 5 and less than 7:

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = from n in numbers
where (n>5 && n<7)
select n;
Console.WriteLine("Print item where item is greater than 5 and less than 7");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
Console.ReadLine();
}


We can check equality using the "=" operator to find an exact match with our search criteria. The code below will display the items where the item value is 0.

Static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = from n in numbers where n==0 select n;
Console.WriteLine("Select particular item from list");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
Console.ReadLine();
}


Sometimes its very necessary to do sorting of a collection or integer array. The code below sorts an integer array in descending order. We only need to use the descending keyword with the orderby clause.

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = from n in numbers orderby n descending select n;
Console.WriteLine("Sort integer array in descending order");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
Console.ReadLine();
}

following example to sort integer array in ascending order, use ascending keyword for that.

static void Main(string[] args)
{

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = (from n in numbers
orderby n ascending select n);
Console.WriteLine("Sort integer array in ascending order");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
Console.ReadLine();
}


The code below will show how to display a maximum of 3 items from an integer array. The process is very simple; at first arrange the list in descending order, then pick the first 3 elements from it using the Take() function.
static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = (from n in numbers
orderby n descending
select n).Take(3);
Console.WriteLine("Sort integer array in descending order");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
Console.ReadLine();
}


The following example shows how to find the second highest number from the array. The process is very simple; just arrange the array in descending order, then take the top two using the Take(n) function and use the Skip(n) function to skip the first item. And hence we can get the second highest number .

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = (from n in numbers
orderby n descending
select n).Take(2).Skip(1);
Console.WriteLine("Find Second highest number from LIST using LINQ");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
Console.ReadLine();
}

If we want to show the sum of only the even numbers then we can use the Sum() function with even selection criteria.
static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = (from n in numbers
where(n % 2 == 0)
select n).Sum();
Console.WriteLine("Make sum of all even number");
Console.WriteLine(lowNums);
Console.ReadLine();
}


The same is true for selecting the sum of all odd numbers in an integer array. Use the Sum() function with odd number selection criteria.

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = (from n in numbers where(n % 2 != 0)
select n).Sum();
Console.WriteLine("Make sum of all even number");
Console.WriteLine(lowNums);
Console.ReadLine();
}

If it is necessary to count the number of items in an array then the code below is for that. Use the Count() function with all the item selection criteria.

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = (from n in numbers
select n).Count();
Console.WriteLine("Count number of item in Array");
Console.WriteLine(lowNums);
Console.ReadLine();
}


The code below is for displaying the average of all numbers from an integer array. Just use the Average() function with all selection criteria.

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums = (from n in numbers
select n).Average();
Console.WriteLine("Make average of all item in Array");
Console.WriteLine(lowNums);
Console.ReadLine();
}

We can show all distinct items from a list using the Distinct() function. Use the Distinct() function with all selection criteria of the list.

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ,5,1};
var lowNums = (from n in numbers
select n).Distinct();
Console.WriteLine("Select distinct item in Array");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
Console.ReadLine();
}


We can pick the nth item from an integer array using LINQ . It's just like picking an element of an array using an index. We need to use the ElementAt(n) function to point to the nth element.`

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ,5,1};
var lowNums = (from n in numbers
select n).ElementAt(2);
Console.WriteLine("Select nth item from list");
Console.WriteLine(lowNums);
Console.ReadLine();
}


Using LINQ we can identify the source data type. Use the GetType() function to do that.
Static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ,5,1};
var lowNums = (from n in numbers select n).GetType();
Console.WriteLine("Get type of collection");
Console.WriteLine(lowNums);
Console.ReadLine();
}


Sometimes it's very necessary to find the maximum number of an array, The good news is that LINQ provides the Max() function to get it done.

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ,5,1};
var lowNums = (from n in numbers
select n).Max();
Console.WriteLine("Select miximum item from list");
Console.WriteLine(lowNums);
Console.ReadLine();
}

Like Max you can use the Min() function to find the minimum item from a list.

static void Main(string[] args)
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 ,5,1};
var lowNums = (from n in numbers
select n).Min();
Console.WriteLine("Select minimum item from list");
Console.WriteLine(lowNums);
Console.ReadLine();
}

No comments: