1. Trang chủ
  2. » Công Nghệ Thông Tin

a0012 practical approach to lin morebook vn 2938

7 2 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Practical approach to Language Integrated Query (LINQ)
Tác giả Vaidhyanathan R, Kaaleeswari K
Chuyên ngành Computer science
Thể loại White paper
Năm xuất bản 2009
Định dạng
Số trang 7
Dung lượng 83,88 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Practical approach to Language Integrated Query LINQ 26/06/2009 HEALTHCARE Vaidhyanathan... Introduction Language-Integrated Query LINQ is a new feature introduced in visual studio 20

Trang 1

Practical approach to Language Integrated Query (LINQ)

26/06/2009

HEALTHCARE

Vaidhyanathan R

Kaaleeswari K

vaidhya.r@tcs.com

kaaleeswari.k@tcs.com

Trang 2

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

Trang 3

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

Trang 4

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

Trang 5

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

{

staticvoid Main()

{

// The Three Parts of a LINQ Query:

// 1 Data source

string[] names = newstring[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>

[Ascending | Descending] Control the ordering of the results Aggregate count([<expression>]),

sum(<expression>), min(<expression>), max(<expression>), avg(<expression>)

Aggregate the source items

Projection select <expression> Shaping the output

Trang 6

Extracting data from existing XML file

1 Add the following code to the Page_Load event and run the application

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);

Trang 7

References

1) http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx

2) Head First C# book

3) http://www.codeproject.com/KB/linq/LINQtoXML.aspx

4) http://dotnetslackers.com/articles/csharp/IntroducingLINQ1.aspx

5) http://www.codeguru.com/csharp/csharp/net30/article.php/c13715

6)

http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

Ngày đăng: 04/12/2022, 09:47