LINQ to dataset study Note 1

Source: Internet
Author: User
LINQ to Dataset

In addition. net integration, through ADO. net to obtain data, and then query the data through LINQ, so as to implement complex query of the Data. To be understood as querying the data saved in dataset through LINQ, generally, den provides the following steps:

1.Obtain the dataset or datatable Data Source, LINQ to dataset query data in dataset or datatable through LINQ. Therefore, you must first prepare the dataset or datatable data source. You can use ADO. net can be obtained directly from the database, XML can be obtained from the XML file, or any other form of data source, you can even directly create and fill in dataset or able objects in the memory.

2.Converts a able to ienumerable <t> type,Because LINQ can only perform operations on the ienumerable <t> and iqueryable <t> interfaces, and datatable does not implement these two interfaces, you cannot directly operate on them,

In LINQ to dataset, the asenumerable () method extended by datatableextensions is used to obtain an equivalent object that implements the ienumerable <t> interface from datatable.

3.Use a LINQ statement for query.You can use the query syntax and method syntax to compile a query in the LINQ to dataset. You can perform any query operations that are allowed by ienumerable <t>.

4.Use query resultsYou can get an ienumerable <t> object, use foreach to traverse all elements, or use it as a data source for the second query.

The following is an example of the above process. dataset in the example is written directly in the memory through code and is not obtained from the database. Since dataset itself is a set of able, it can contain one or more datatable relationships with them. In fact, LINQ to dataset is actually a query of the datatable, not a query of the datatable.

A dataset usually contains one or more data tables and a set of relationships between them. In fact, it can be seen as a microcosm of the database. LINQ to dataset is also used to query one or more data tables. These data tables can be from a single data set or multiple data sets. You must use the asenumerable () method of the able class to query the data of the datatable. This method converts the datatable to an enumerative data set of ienumerable <datarow> type.

Single Table query:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Data;

Namespace consoleapplication1
{
Class linqtest
{
Public static void main ()
{
Linqtodataset. create_dataset ();
Linqtodataset. printdata ();
}
}
Class linqtodataset
{
Public static dataset create_dataset () // create a data source in the memory
{
String [] nameset = {"Wang Xia", "Zhang San", "Li Si", "Li Hua", "Wang Wu", "Lu 6", "xia Qi ", "Wu ba "};
String [] sexset = {"female", "male", "male", "female", "male "};
Int [] ageset = {18, 20, 21, 22, 19, 20, 25, 24 };
Dataset DS = new dataset ();
Datatable dt = new datatable ("people ");
DS. Tables. Add (DT );
DT. Columns. Add (New datacolumn ("name", type. GetType ("system. String ")));
DT. Columns. Add (New datacolumn ("sex", type. GetType ("system. String ")));
DT. Columns. Add (New datacolumn ("Age", type. GetType ("system. int32 ")));
For (INT I = 0; I <nameset. length; I ++)
{
Datarow DR = DT. newrow ();
Dr ["name"] = nameset [I];
Dr ["sex"] = sexset [I];
Dr ["Age"] = ageset [I];
DT. Rows. Add (DR );
}
Return Ds;
}

Public static void printdata () // print data
{
Dataset DS = create_dataset ();
Datatable dt = Ds. Tables ["people"];
VaR query1 = from item in DT. asenumerable () select item;
Foreach (VAR item in query1)
{
Console. writeline ("Name: {0}, Gender: {1}, age: {2}", item. field <string> ("name"), item. field <string> ("sex"), item. field <int> ("Age "));
}
VaR query2 = from data in DT. asenumerable () Select data. Field <string> ("name ");
Foreach (VAR item in query2)
{
Console. writeline ("Name: {0}", item );
}
VaR query3 = from data in DT. asenumerable () where data. Field <int> ("Age")> = 22 select data;
Foreach (VAR item in query3)
{
Console. writeline ("Name: {0}, Gender: {1}, age: {2}", item. field <string> ("name"), item. field <string> ("sex"), item. field <int> ("Age "));
}
Console. Readline ();

}
}
}


Modify Field data in a table

In the sample code in the previous chapter, only datarowextensions is used. the field () method is used to obtain the data of fields in the data table. Of course, you may need to modify the data in the data table by using the LINQ to dataset method. This section describes how to use setfield () to modify the data.

Public static void updatedata ()
{
Dataset DS = create_dataset ();
Datatable dt = Ds. Tables ["people"];
Foreach (VAR item in DT. asenumerable ())
{
Int age = item. Field <int> ("Age ");
Item. setfield <int> ("Age", age + 2 );
}
Foreach (VAR item in DT. asenumerable ())
{
Console. writeline ("Name: {0}, Gender: {1}, age: {2}", item. field <string> ("name"), item. field <string> ("sex"), item. field <int> ("Age "));
}
}

Use Data View dataview

Dataview is a commonly used form of data storage in read-only mode. It can be bound to the UI to provide users with Dynamic Data Query functions. Dataview can be obtained from datatable or dataset. In addition, in. net3.5, dataview can also be obtained through LINQ queries. In
To dataset, you can use the ableableextensions. asdataview () Extension Method to create a dataview object corresponding to the data source from the datatable or LINQ query.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.