- LINQ unifies data access, whatever the source of data, and allows mixing data from different kind of sources.
- LINQ provides a uniform way to retrieve data from any object that implements the Enumerable interface.
Microsoft has introduced LINQ with many goals in mind to bring down the way data is accessed from different datasource, by using LINQ we can use a uniform query to perform operations on various datasource, it also answers disadvanatges of SQL
Disadvantages of SQL
- Compile time support: You cannot know until run time whether the string is correct because the compiler treats it as a simple string.
- Design time support: No tool offers IntelliSense support for these command strings
To solve this queries should be integrated into a language, which then can be verified both during compile time as well as runtime and important part is it can offer intellisence support also.
LINQ addresses both these issues. Because the queries are integrated into the language, they are verified during compilation and you have IntelliSense support for them.
There are various data providers which is shown below
- LINQ to Objects
- LINQ to XML
- LINQ to SQL
- LINQ to DataSet
- LINQ to Entities
LINQ to Objects
LINQ to Objects allows .NET developers to write “queries” over collections of objects.
let me take 2 example one without LINQ and another using LINQ
Now let us separate the words whose length is greater then 5,To perform this we should iterate all the members of an array which is tedious and with more CPU Utilizing.
class HelloWorld
{
static void Main()
{
string[] words = new string[] {“Sadhana”, “Naveen”, “Praveen”, “Pankaj”, “Abhilash” };
foreach (string word in words)
{
if (word.Length <= 5)
Console.WriteLine(word);
}
}
}
The same can be achieved using LINQ
class Hello_LINQ_to_Objects_Demo1
{
static void Main()
{
String[] champion_names = {“Sadhana”, “Naveen”, “Praveen”, “Pankaj”, “Abhilash” };
var short_name = from name in champion_names
where name.Length >= 5
select name;
foreach (var word in short_name)
Console.WriteLine(word);
Console.ReadLine();
}
}
Take this example which which uses LINQ with class
class CustomerDetails
{
public string customer_name;
public int customer_id;
public string customer_address;
public double customer_purchase_cost;
public string customerName
{
get{return customer_name;}
set{customer_name=value;}
}
public int customerId
{
get{return customer_id;}
set{customer_id=value;}
}
public string customerAddress
{
get{return customer_address;}
set{customer_address=value;}
}
public double customerPurchaseCost
{
get{return customer_purchase_cost;}
set{customer_purchase_cost=value;}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(“Enter the Customer Name”);
string name = Console.ReadLine();
var customerdetails = new List<CustomerDetails>
{
new CustomerDetails
{
customerName=“Naveen”,
customerId=110,
customerAddress=“Bannerghatta Road Bangalore 560076″,
customerPurchaseCost=100000
},
new CustomerDetails
{
customerName=“Praveen”,
customerId=111,
customerAddress=“Adugudi Road Bangalore 560076″,
customerPurchaseCost=100000
},
new CustomerDetails
{
customerName=“Sadhana”,
customerId=112,
customerAddress=“JP Nagar Bangalore 560076″,
customerPurchaseCost=100000
},
};
var Found = from details in customerdetails
//where details.customerName == “Sadhana”
where details.customerName == name
select details.customerAddress;
//details.customerId & details.customerAddress;
// Display results.
foreach (var Result in Found)
Console.WriteLine(“Address: “ + Result.ToString());
Console.ReadLine();
}
No comments:
Post a Comment