LINQ can be written in all language of Visual Studio .But syntax
are different for languages different.LINQ is available from Visual Studio
2010 and (.Net Framework 3.5) and upper version. Now , we will learn how to
create a setup LINQ environment.
To do this , you have to install Visual Studio first. There are two options, download Visual Studio Express or purchase license version of Visual Studio for professional uses. In the example we have downloaded Visual Studio 2015 . First we need to click on setup, the setup will start.
Installation has two option "default" or "customs" , you need to choose default options, Visual Studio will proceed, progress bar will show the progress of installation.
After that you will see a message that the installation is completed. If you have downloaded Express version, you need to sign up Microsoft to smooth performance of Visual Studio.
To create a console application
click 1)File --> New Project
2)Choose --> Console Application
you will get this editor. Please keep in mind, LINQ can be used for desktop application, web application , mobile application also. For the purpose of training we have chosen console application.
Below is the example of LINQ. There are three example. The first example is simple LINQ query over an array. The second example of utility of IEnumerable interface. IEnumerableis heart of LINQ queries, you will learn details it in the letter chapter.The third example is how complex object is handle by LINQ queries.
Example
foreach (var c in AllCountry)
To do this , you have to install Visual Studio first. There are two options, download Visual Studio Express or purchase license version of Visual Studio for professional uses. In the example we have downloaded Visual Studio 2015 . First we need to click on setup, the setup will start.
Installation has two option "default" or "customs" , you need to choose default options, Visual Studio will proceed, progress bar will show the progress of installation.
After that you will see a message that the installation is completed. If you have downloaded Express version, you need to sign up Microsoft to smooth performance of Visual Studio.
To create a console application
click 1)File --> New Project
2)Choose --> Console Application
you will get this editor. Please keep in mind, LINQ can be used for desktop application, web application , mobile application also. For the purpose of training we have chosen console application.
Below is the example of LINQ. There are three example. The first example is simple LINQ query over an array. The second example of utility of IEnumerable interface. IEnumerableis heart of LINQ queries, you will learn details it in the letter chapter.The third example is how complex object is handle by LINQ queries.
Example
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Linq;
using
System.Collections;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(string[]
args)
{
string[]
country = { "India",
"Brazil",
"China",
"USA",
"Pakistan",
"South
Afrika",
"Japan",
"Russia",
"Poland",
"Sweden"
};
/*
Example 1 (Simple Select Statement) */
var
AllCountry = from
c in
country select
c;
Console.WriteLine("List
of Country");
Console.WriteLine("----------------");
foreach (var c in AllCountry)
{
Console.WriteLine(c);
}
Console.ReadLine();
/*
Example 1 (All output as reverse list) */
var
uppercseCountry = country.Reverse().ToArray();
Console.WriteLine("Revese
List of Country");
Console.WriteLine("----------------");
foreach
(var
c in
uppercseCountry)
{
Console.WriteLine(c);
}
Console.ReadLine();
/*
Example 3 (LINQ on Array List) */
ArrayList
myList = new
ArrayList();
myList.Add(
new
student
{
name
= "John",
roll
= 12,
age
= 9
});
myList.Add(
new
student
{
name
= "John1",
roll
= 14,
age
= 8
});
myList.Add(
new
student
{
name
= "John2",
roll
= 13,
age
= 7
});
myList.Add(
new
student
{
name
= "John3",
roll
= 15,
age
= 8
});
var
query = from
student
s in
myList
where
s.age > 7
select
s;
Console.WriteLine("List
of Student Over 7 Years of Age");
Console.WriteLine("--------------------------------------");
foreach
(student
c in
query)
{
Console.WriteLine("Name
: "+c.name);
Console.WriteLine("Roll
: "
+ c.roll);
Console.WriteLine("Age
: "
+ c.age);
Console.WriteLine("\n");
}
}
Console.ReadLine();
}
}
}
public
class
student
{
public
string
name { get;
set;
}
public
int
roll { get;
set;
}
public
int
age { get;
set;
}
}
After you have written the code, press F5 run the code. You will get the output one by one as you press the key.
Output :
After you have written the code, press F5 run the code. You will get the output one by one as you press the key.
Output :
List of Country
----------------
India
Brazil
China
USA
Pakistan
South Afrika
Japan
Russia
Poland
Sweden
Revese List of
Country
----------------
Sweden
Poland
Russia
Japan
South Afrika
Pakistan
USA
China
Brazil
India
List of Student
Over 7 Years of Age
--------------------------------------
Name : John
Roll : 12
Age : 9
Name : John1
Roll : 14
Age : 8
Name : John3
Roll : 15
Age : 8