What Is LINQ?

LINQ :
      LINQ is a programming model that introduces queries as a first-class concept into any
Microsoft .NET language. However, complete support for LINQ requires some extensions in
the language used. These extensions boost productivity, thereby providing a shorter, meaningful,
and expressive syntax to manipulate data.
Following is a simple LINQ query for a typical software solution that returns the names of customers
in Italy:

var query =
from c in Customers
where c.Country == "Italy"
select c.CompanyName;
 Do not worry about syntax and keywords (such as var) for now. The result of this query is a
list of strings. You can enumerate these values with a foreach loop in C#:
foreach ( string name in query ) {
Console.WriteLine( name );
}

Both the query definition and the foreach loop just shown are regular C# 3.0 statements. At
this point, you might wonder what we are querying. What is Customers? Is this query a new
form of embedded SQL? Not at all. The same query (and the following foreach code) can be
applied to an SQL database, to a DataSet, to an array of objects in memory, or to many other
kinds of data. Customers could be a collection of objects:
Customer[] Customers;
Customers could be a DataTable in a DataSet:
DataSet ds = GetDataSet();
DataTable Customers = ds.Tables["Customers"];

Customers could be an entity class that describes a physical table in a relational database:
DataContext db = new DataContext( ConnectionString );
Table<Customer> Customers = db.GetTable<Customer>();
Finally, Customers could be an entity class that describes a conceptual model and is mapped to
a relational database:
NorthwindModel dataModel = new NorthwindModel();
ObjectQuery<Customer> Customers = dataModel.Customers;
As you will see, the SQL-like syntax used in LINQ is called a query expression. Languages that
implement embedded SQL define only a simplified syntax to put SQL statements into a different
language, but these statements are not integrated into the language’s native syntax and
type system. For example, you cannot call a function written using the host language in the
middle of an SQL statement, although this is possible in LINQ. Moreover, LINQ is not limited
to querying databases, as embedded SQL is.

0 comments:

Post a Comment