Tải bản đầy đủ (.pdf) (7 trang)

a0012 practical approach to lin morebook vn 2938

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (83.88 KB, 7 trang )

Practical approach to Language Integrated
Query (LINQ)
26/06/2009

HEALTHCARE
Vaidhyanathan. R
Kaaleeswari. K



TCS Public


Practical approach to Language Integrated Query (LINQ)

Introduction
Language-Integrated Query (LINQ) is a new feature introduced in visual studio
2008 and .NET Framework 3.5. Developers can write the queries in the code to
retrieve the data from various types of data sources like SQL, XML and XQuery.
Earlier developers have to learn the syntax for writing queries for each data
source. LINQ simplifies by offering a model to work with the data across various
kinds of data sources.

LINQ Architecture

Internal Use

2


Practical approach to Language Integrated Query (LINQ)



Language Features that Enable LINQ
LINQ makes heavy use of Generics. Additionally, there were a number of features
added to the Visual Basic and C# languages specifically to support LINQ. The
following contains a partial list of the language features that help enable LINQ
and a brief description of each:







Type Inference: Shorthand indicating the variables type is the
compile time type of the right hand assignment
Extension methods: Extending an existing value or reference type
without deriving a new type
Object Initializer: Short form of object initialization syntax that
generates the equivalent code
Anonymous types: Create statements without constructing a method
or type
Lambda expressions: Concise way of creating inline methods
Query expressions: SQL-like statements within code for
manipulating objects

Internal Use

3



Practical approach to Language Integrated Query (LINQ)

Flavors of LINQ
There are a variety of flavors of LINQ for accessing and manipulating different
data sources. The trailing list contains some of the data domains provided by
Microsoft. Some of these will be topics of future .NET Nuts and Bolts articles.






LINQ to Objects: Manipulates collections of objects
LINQ to DataSets: Manipulates a DataSet using LINQ
LINQ to SQL: Maps between custom types and a physical database table
schema
LINQ to Entities: Uses a conceptual Entity Data Model to create a
conceptual model of a physical database
LINQ to XML: Allows querying and manipulation of XML

Internal Use

4


Practical approach to Language Integrated Query (LINQ)

Introduction to LINQ Syntax
LINQ Query operation contains three parts
1. Obtain the data source.

2. Create the Query.
3. Execute the Query.
LINQ Example
class LINQExample
{
static void Main()
{
// The Three Parts of a LINQ Query:
// 1. Data source.
string[] names = new string[4] { “TOM”, “DAN”, “ADAMS”, “BERNARD” };
// 2. Query creation.
// nameQuery is an IEnumerable<string>
var nameQuery =
from name in names
where name == “TOM”
select name;
// 3. Query execution.
foreach (int name in nameQuery)
{
Console.Write("{0,1} ", name);
}
}
}
The following table outlines some of the options available with LINQ syntax.
Destination var <variable> =

Using type inference to
assign the resulting
value(s)


Source

from <item> in <data source>

Information source
providing a set of item(s)

Filter

where <expression>, distinct

Expression specifying the
selection criteria

Order

order by <expression>, <expression> Control the ordering of
[Ascending | Descending]
the results

Aggregate count([<expression>]),
sum(<expression>),
min(<expression>),
max(<expression>),
avg(<expression>)

Aggregate the source
items

Projection select <expression>


Shaping the output

Internal Use

5


Practical approach to Language Integrated Query (LINQ)

Extracting data from existing XML file.

1. Add the following code to the Page_Load event and run the application
XDocument xmlDoc = XDocument.Load(@"C:\Documents and
Settings\164164\My Documents\Visual Studio 2008\Backup
Files\SampleWebforXML\SampleWebforXML\sample.xml");
var q = from c in xmlDoc.Descendants("employee")
where (int)c.FirstAttribute == 3
select c.Element("name").Value;
foreach (var p in q)
Response.Write(p);

Internal Use

19


Practical approach to Language Integrated Query (LINQ)

References

1)
2)
3)
4)
5)
6)

/>Head First C# book
/> /> /> />
Internal Use

20



×