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

Thao tác dữ liệu Sử dụng LINQ docx

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 (535.25 KB, 16 trang )




Manipulating Data
Using LINQ
Chapter 7

Any application that displays dynamic information has to
retrieve data from various data sources, such as a flat file,
an XML file, or a database. To access data from these data
sources, programmers need to implement different
techniques. This complicates the task of programmers
because they need to master all the techniques. However,
the task of programmers can be simplified by
implementing a standard technique to access data from
various data sources. Such a standard technique is
provided by Language-Integrated Query (LINQ).
This chapter provides an overview of LINQ. In addition, it
discusses how to access data from disparate data sources.

In this chapter, you will learn to:
 Identify the basics of LINQ
 Access data from disparate data sources by using
LINQ
Objectives



NIIT Manipulating Data Using LINQ 7.3

When creating a dynamic Web application, you need to retrieve data from various data


sources, such as a flat file, an XML file, or a database. To access data from these disparate
data sources, you need to learn and implement different techniques. For example, to
access data from files, you need to implement input/output streams and to access data
from a database, you need to implement SQL. This complicates your task as a
programmer. This task can be simplified by using LINQ.
LINQ provides a standard way to access data from disparate data sources. Using LINQ,
you can access data from disparate data sources by writing a standard query, LINQ query.
This eases your task because you need not learn different technologies to access data. In
addition, LINQ provides various facilities such as stronger typing, compile-time checking,
and improved debugger support.
The following figure shows a model of .NET LINQ.

Model of .NET LINQ
Identifying the Basics of LINQ
7.4 Manipulating Data Using LINQ NIIT
The .NET Language-Integrated Query model consists of all the languages in which you
can create your Web application. It includes the components such as LINQ to ADO.NET,
LINQ to XML, and LINQ to objects, using which you can access the data from data
sources such as database, an arraylist, and an XML.
To retrieve data from data sources, you need to write a LINQ query. Therefore, you need
to know the different components of a LINQ query.



A LINQ query is an expression that retrieves data from a data source. It enables you to
write a standard query expression to retrieve data form disparate data sources. It also
allows you to filter, sort, group, and join the results of the query by using additional
clauses in the query.
Accessing Data by Using a Simple LINQ Query
To access data by using LINQ, you need to perform the following steps:

1. Specify the data source.
2. Create the query.
3. Execute the query.
Specifying the Data Source
In a LINQ query operation, the first step is to specify the data source. The data source in a
LINQ query can be an XML file, an array, a collection, or a database. If the data source
that you want to use is an array, you can specify the data source by using the following
statement:

string[] customers = new string[2] {John, Mary};
In the preceding statement, the array, customers, is the data source.
Similarly, if the data source that you want to use is an XML file, you can specify the data
source by using the following statement:

XElement customers = XElement.Load(@"c:\myCustomerList.xml");
In the preceding statement, the XElement object, customers, is the data source.
After specifying the data source, you need to create a query expression to retrieve or
manipulate data from the specified data source.
Understanding the LINQ Query
NIIT Manipulating Data Using LINQ 7.5
N
ot
e
N
ot
e
Creating the Query
A query describes what information is to be retrieved from the specified data source. It
must include the
from and select clauses to retrieve data. The from clause specifies the

data source from where the data is to be retrieved and the
select clause specifies the type
of the returned elements. Consider the following query:

var query = from <type of the value returned> cus in customers
select cus;
The preceding query contains the from and select clauses. The from clause specifies the
range variable
cus and the data source Customers, and the select clause specifies the
type of the values that will be returned by the query. The query is stored in a query
variable,
query. It is initialized with a query expression.


The range variable acts as an iteration variable in a foreach loop. When the query is
executed, the range variable is served as a reference of each item stored in the data source,
such as a database.




The var keyword is a new keyword that can infer its data type from the resulting set. A
var type variable needs to be initialized at the time of declaration. The var keyword
cannot be used when declaring the member variables of a class. Consider the following
examples:
var one = 1; //infers an int
var two = "2"; // infers a string
var five = null; // This results in a compilation error because
//the data type is not specified in the Right hand side.



Executing the Query
In LINQ, the query variable only stores the query. The actual execution of the query is
deferred until the query variable is iterated by using the
foreach loop. The foreach loop
is the place where the query results are retrieved. Consider the following code snippet:

//Query execution
foreach (var cus in query)
{
Response.Write(cus);
}
7.6 Manipulating Data Using LINQ NIIT
In the preceding code snippet, the iteration variable cus holds each value, one at a time, in
the same sequence as returned by the query.
The query variable only stores the query that is to be executed. Therefore, you can execute
the query variable as many times as you want. It is useful in situations where the database
is constantly updated. In such a situation, you can create a query variable and execute it at
some interval to retrieve the updated data.
The execution of all the queries is not deferred till query variable is iterated by using the
foreach loop. The queries that perform aggregate functions such as Max and Count over a
range of source elements must first iterate over those elements. Such queries execute
without explicitly using the
foreach loop. This is because such queries itself use the
foreach loop in order to return the result. These queries return a single value. Consider
the following example:

var evenNumbers = from num in numbers
where (num % 2) == 0
select num;

int Count = evenNumbers.Count();
In the preceding example, the foreach loop has not been used to execute the query. It gets
executed automatically when the
Count function is used.
Consider the following example in which the LINQ query is used to retrieve data from an
ArrayList object:

public partial class Default : System.Web.UI.Page
{
public class customer
{
public string Name { get; set; }
public string City { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
ArrayList arrList = new ArrayList();
arrList.Add(
new customer
{
Name = "Svetlana", City = "London"
}
);
arrList.Add(
new customer
{
Name = "Claire", City="Paris"
}
);
arrList.Add(

new customer
{
NIIT Manipulating Data Using LINQ 7.7
Name = "Cesar", City="New York"
}
);
var query = from customer cus in arrList
select cus;
foreach (var cus in query)
{
Response.Write (cus.Name + " lives in " + cus.City
+ "<br>");
}
}
}
In the preceding example, a class customer with two properties, Name and City, is
created. After creating the class, an object of the
ArrayList class is created and three
objects of the type
customer are added to the ArrayList object. The ArrayList object is
the data source from which the data is retrieved by using the LINQ query. The LINQ
query retrieves all the data from the data source.
Using LINQ Queries to Filter, Sort, Group, and Join Data
In addition to selecting data, a query can perform the following operations on data:
 Filtering
 Ordering
 Grouping
 Joining
Filtering
To retrieve data depending on certain criteria or condition, you need to filter the data.

Filtering causes the query to return only the data that matches with the specified
condition. The condition to retrieve data can be specified by using the
where clause. For
example, the following code snippet returns the data of the customers who live in
London:

var query = from <type of the value returned> cus in customers
where cus.City == "London"
select cus;
You can also use the logical operators in the where clause to apply two or more
conditions. For example, the following code snippet returns the data of the customers who
live in
London and have last name as Darcy:

var query = from <type of the value returned> cus in customers
where cus.City == "London" && cust.Name == "Svetlana"
select cus;
7.8 Manipulating Data Using LINQ NIIT
N
ot
e

Ordering
LINQ queries can also be used to retrieve data and display it in a sorted manner. This can
be done by using the
orderby clause. The orderby clause enables a user to sort the
retrieved data in ascending or descending order. For example, the following code snippet
returns the data for the customers who live in London. The data is sorted by customer
names in alphabetical order:


var query =
from <type of the value returned> cus in customers
where cus.City == "London"
orderby cus.Name descending
select cus;


The default value for the orderby clause is ascending.
Grouping
You can also retrieve data and group it by using the
group clause. For example, the
following code snippet returns the data for all the customers, grouped on the basis of the
cities they live in:

var query = from <type of the value returned> cus in customers
group cus by cus.City;
When you end a query with a group clause, the results take the form of a list of lists. Each
element in the list is an object that has a Key member and a list of elements that are
grouped under that key. When you iterate over a query that produces a sequence of
groups, you must use a nested
foreach loop. The outer loop iterates over each group, and
the inner loop iterates over each member of the group. For example, to execute the
preceding query and to display the results, you need to use nested foreach loops as shown
in the following code snippet:

foreach (var cus in query)
{
foreach (var i in cus)
{
Response.Write(i.Name + " lives in " + i.City +

"<br>");
}
}

NIIT Manipulating Data Using LINQ 7.9
Joining
It is possible that the data you need to retrieve exists in more than one table/object. For
example, you need to retrieve information regarding the orders placed by a customer. The
information regarding the customers is stored in the customers table/object and the
information regarding the orders is stored in the orders table/object. Both the
tables/objects must have a common field, which is required to join the tables/objects. To
retrieve the required information, you need to join the data from both the tables/objects.
This can be done by using the
join clause, as shown in the following code snippet:

var query = from <type of the value returned> cus in customers join
<type of the value returned> ord in Order on cus.order_id
equals ord.order_id
Select new {CustomerName=cus.Name,
OrderAmount=Order.Amount};
In the preceding code snippet, two ArrayList objects customers and Order are the data
sources. These ArrayList objects contain information about the cutomers and the orders
placed by them. Both the objects have a common field
order_id, which is required to join
data from these objects. The data from the preceding query can be retrieved by using the
following
foreach loop:

foreach (var cus in query)
{

Response.Write(cus.CustomerName + " has the order amount " +
cus.OrderAmount + "<br>");
}


7.10 Manipulating Data Using LINQ NIIT

In Web applications, the data that is to be displayed on the Web pages may exist in
disparate data sources. These data sources can be objects, databases, or XML files. You
can access data from these data sources either by using the LINQ queries or by using the
LINQ data source control.


LINQ provides a standard query for retrieving data from disparate data sources. These
queries can be used to access data from:
 Objects
 Databases
 XML files
Accessing Data from Objects
Data from objects can be accessed by using LINQ to Objects. LINQ to Objects provides
an easy way to create a query for accessing data from an object collection such as an
ArrayList by writing
foreach loops.
To use LINQ to query an object collection, you need to declare a range variable. The type
of the range variable should match the type of the objects in the collection. For example,
if you create an ArrayList that stores objects of type
customers, the range variable should
also be of the
customers type, as shown in the following code snippet:


var query = from customers cust in arrList
select cust;
In the preceding code snippet, a range variable cust is of the type customers. By
declaring a range variable, each item in the ArrayList is typecasted to a
customers object.
The following example describes how to access data from a string array that contains the
names of students in a class:

string[] arr = new string[] { "Peter", "Sam" , "Philip"};
var query = from string person in arr
select person;
foreach (var p in query)
{
Response.Write(p + "<BR>");
}


Accessing Data from Disparate Data Sources
A
ccessing Data by Using the LINQ Queries
NIIT Manipulating Data Using LINQ 7.11
Accessing Data from Databases
A Web application accesses most of the data from databases. To access data from a
database, you can use LINQ to ADO.NET. LINQ to ADO.NET includes the following
technologies to access data:
 LINQ to dataset
 LINQ to SQL
LINQ to Dataset
A Dataset is a collection of cached data presented in a tabular form. The data from a
Dataset can be retrieved by using LINQ. LINQ to Dataset is basically used in situations

where you have used ADO.NET for accessing data from a database. Now, you want to
implement LINQ queries in the application to get the benefits such as compile time
checking of the query syntax and better performance. However, you want minimal
changes in the coding. In such a situation, you can use LINQ to ADO.NET because by
using LINQ to ADO.NET, you just need to change the data retrieval code that was used to
access data from the Dataset. The rest of the code will remain the same.

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection MyConn = new SqlConnection();
MyConn.ConnectionString = "Data Source=172.23.3.59;Initial
Catalog=Music;User Id=sa; Password=password@123";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("Select * from
albums",MyConn);
MyConn.Open();
da.Fill(ds);
DataTable album= ds.Tables[0];
var q = from d in album.AsEnumerable()
select d;
foreach (var i in q)
{
Response.Write(i.Field<string>("Album_Name") + "<br>");
}
}
In the preceding example, LINQ to Dataset is used to retrieve data from the Dataset and
display it on the Web page. Notice the use of the
AsEnumerable() method. It changes the
compile time type of a query to
IEnumerable<T>. This means that the type can be used in

a
foreach loop.
LINQ to SQL
LINQ to SQL provides a run-time infrastructure for managing relational data as objects.
In LINQ to SQL, the data model of a relational database is mapped to an object model
7.12 Manipulating Data Using LINQ NIIT
created by the developer. While querying data by using LINQ to SQL, it translates the
language-integrated queries in the object model defined by the developer and sends the
data to the database for execution. The database returns the result, and LINQ to SQL
translates the result back into the form of objects that can be used in a programming
language to query data. This helps in integrating the object-oriented features such as
classes and methods with SQL. The methods written to query data can be reused in any
application because these methods are generic.
In addition, IDE such as Microsoft Visual Studio provides the Object Relational Designer
(O/R designer), which provides a visual designer surface for creating LINQ to SQL entity
classes and relationships that are based on objects in a database.


Activity 7.1: Using LINQ to Retrieve and Store Data
LINQ to SQL entity classes are also used by the ASP.NET Dynamic Data to determine
the user interface that is based on the LINQ to SQL data model. ASP.NET Dynamic Data
is a framework that enables a user to create data-driven ASP.NET Web applications with
minimal or no coding. ASP.NET Dynamic Data does this by automatically discovering
data-model metadata at run time and rendering the user interface according to it. The
data-driven ASP.NET Web applications created by using ASP.NET Dynamic Data
provides:
 Data access operations such as create, update, remove, and display, relational
operations, and data validation.
 Built-in support for foreign-key relationships.
 The ability to customize the user interface rendered to display and edit specific data

fields.
 The ability to customize data field validation.


Task 7.1: Creating an ASP.NET Dynamic Data Website
Accessing Data from XML Files
XML is widely used to exchange data between various applications. LINQ to XML is a
programming interface that enables you to access data stored in XML files. The following
example shows the code in an XML file,
CustomersDetails.Xml:

NIIT Manipulating Data Using LINQ 7.13
CustomersDetails.Xml:
<?xml version="1.0" encoding="utf-8" ?>
<CustomersDetails>
<Customer>
<CustomerID>C001</CustomerID>
<Name>Svetlana</Name>
<City>London</City>
</Customer>
<Customer>
<CustomerID>C002</CustomerID>
<Name>Claire</Name>
<City >Paris</City>
</Customer>
<Customer>
<CustomerID>C003</CustomerID>
<Name>Cesar</Name>
<City>New York</City>
</Customer>

</CustomersDetails>
To access the data from the CustomersDetails.Xml by using LINQ, you need to write
the following code snippet:

XDocument xmlDoc = XDocument.Load("D:\\Website7\\XMLFile2.xml");
var q = from c in xmlDoc.Descendants("Customer")
select (string)c.Element("CustomerID") + "-" +
(string)c.Element("Name")+"-" + (string)c.Element("City")+"<br>";
foreach (string name in q)
{
Response.Write(name);
}


The LinqDataSource control enables you to connect a data control to a wide variety of
data sources, such as a database, data-source classes, and in-memory collections. The
LinqDataSource control can be connected to any kind of data collection that is stored in a
public field or property. The declarative markup and code to perform data operations in a
LinqDataSource control are the same for all data sources. You can bind data-bound
controls to a LinqDataSource control by using the
DataSourceID property of the data-
bound control.
The following table lists some properties of the LinqDataSource control.

Property Description
ContextTypeName
Gets or sets the name of the type that contains
the property whose value has the data that is
to be retrieved.
A

ccessing Data by Using the LINQ Data Source Control
7.14 Manipulating Data Using LINQ NIIT
Property Description
TableName
Gets or sets the name of the property or field
in the data context object that represents a
data collection.
DeleteParameters
Gets the collection of parameters that are used
during the delete operation.
Delete
Performs the delete operation.
InsertParameters
Gets the collection of parameters that are used
during the insert operation.
Insert
Performs the insert operation.
SelectParameters
Gets the collection of parameters that are used
during the data retrieval operation.
Select
Gets or sets the properties and the calculated
values included in the retrieved data.
UpdateParameters
Gets the collection of parameters that are used
during the update operation.
Update
Performs the insert operation.
Properties of the LinqDataSource Control
You can retrieve data by using the LinqDataSource control, as shown in the following

example:
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="MyMusicDataContext"
TableName="albums">
</asp:LinqDataSource>
In the preceding example, the ContextTypeName attribute sets the name of the type that
contains the property that provides the data. The
TableName attribute sets the name of the
table in the data context object that represents a data collection.


NIIT Manipulating Data Using LINQ 7.15

1. Which of the following loops is used to execute the LINQ query?
a. While loop
b. Foreach loop
c. Do While loop
d. Do loop
2. The ___________ keyword is a new keyword that can infer its data type from the
resulting set.
a.
from
b.
where
c.
var
d.
select
3. Which of the following types of LINQ is used to retrieve data from a collection?
a. LINQ to SQL

b. LINQ to Objects
c. LINQ to dataset
d. LINQ to XML
4. Which of the following types of LINQ provides a run-time infrastructure for
managing relational data as objects?
a. LINQ to SQL
b. LINQ to dataset
c. LINQ to Objects
d. LINQ to XML
5. In which of the following operations, the results take the form of a list of lists?
a. Filtering
b. Grouping
c. Joining
d. Ordering
Practice Questions
7.16 Manipulating Data Using LINQ NIIT


In this chapter, you learned that:
 LINQ provides a standard way to access data from disparate data sources.
 When accessing data by using LINQ, you need to perform the following steps:
z Specify the data source
z Create the query
z Execute the query
 LINQ provides a standard query for retrieving data from disparate data sources.
 LINQ queries can be used to access data from:
z Objects
z Databases
z XML files
 You can also use the LINQ data source control to access data from disparate data

sources.
Summar
y

×