LINQ (LINQ to DataSet)

Source: Internet
Author: User

The DataTable. Select () method uses a filter syntax similar to SQL to extract records of interest from the DataTable. Although Select () can work well, it still has some obvious restrictions. First, it is string-based, that is, possible errors cannot be found during compilation. Secondly, its filtering function is very limited, and it does not provide other features that the LINQ operator can provide, such as sorting, grouping, and projection.

When using LINQ to DataSet, the syntax is essentially the same as that of the query object set. After all, DataSet is only a set of datatables, and able is a set of DataRow (and some other architecture information. However,There is an obvious restriction on DataSet: it does not display strong data types. On the contrary,You need to forcibly convert the field value to the appropriate type..

 

To make these applications a reality, you need to use the Field <T> extension method. It is provided by the DataRowExtensions class in the System. Data namespace. Essentially, Field <T> extends all DataRow objects and allows you to access fields in a strongly typed manner:

String value = dataRow. Field <string> ("FirstName ");

LINQ implements the set of IEnumerable <T>. Neither DataRowCollection nor able has implemented this interface. To make up for this defect, you need to use another extension method, AsEnumerable (), it exposes the IEnumerable <T> set in the DataRow object for the specified able:

IEnumerable <DataRow> rows = dataTable. AsEnumerable ();

 

The following example extracts employee records starting with the letter D as a DataRow object:

IEnumerable <DataRow> matches = from employee
In ds. Tables ["Employees"]. AsEnumerable ()
Where employee. Field <string> ("LastName"). StartsWith ("D ")
Select employee;

This set is not suitable for data binding (it only displays the public attributes of the DataRow object rather than the set of field values ). The problem is that when binding ADO. NET data, it must contain the schema (DataTable can be bound because it contains a Columns set with column titles and other information ).

There are two ways to solve this problem.

Method 1: Use DataTableExtensions. AsDataView () to obtain the DataView for filtering rows:

VarMatches = from employee in ds. Tables ["Employees"]. AsEnumerable ()
Where employee. Field <string> ("LastName"). StartsWith ("D ")
Select employee;
GridEmployees. DataSource = matches.AsDataView ();
GridEmployees. DataBind ();

The LINQ to DataSet expression returns an example of the EnumerableRowCollection <T> class (which implements the IEnumerable Interface ).AsDataView () is an extension method that can only work on EnumerableRowCollection <T> objects. Therefore, in the previous example, you must use the var keyword to define a matched variable or define it as EnumerableRowCollection <DataRow>. If you declare it as IEnumerable <DataRow>, the AsDataView () method cannot be accessed.

 

Method 2: Projection

Var matches = from employee in ds. Tables ["Employees"]. AsEnumerable ()
Where employee. Field <string> ("LastName"). StartsWith ("D ")
Select new
              {
First = employee. Field <string> ("FirstName "),
Last = employee. Field <string> ("LastName ")
};
GridEmployees. DataSource = matches;
GridEmployees. DataBind ();

 

       The two methods are completely equivalent. The DataView method is very useful in non-connected rich client scenarios because it can operate data while continuing to track DataSet changes. The projection method can reduce the number of fields to only a few.

 

Strong DataSet

A strongly typed DataSet provides another solution to lift the limitations of DataSet. Because it is strongly typed, you do not need to rely on the Field <T> and AsEnumerable () methods, which makes the expression more readable.

Var matches = from employee in ds. Employees
Where employee. LastName. StartsWith ("D ")
Select new {First = employee. FirstName, Last = employee. LastName };

 

Null value

The Field <T> method plays an important role in accessing Field values in a strongly typed manner. It also has another useful application: It converts a null Value (expressed by DBNull. Value) into a real null reference. Therefore, you can check whether the Value is a null reference instead of comparing it with DBNull. Value, which makes the LINQ expression more clean.

Var matches = from employee in ds. Tables ["Employees"]. AsEnumerable ()
Where employee. Field <string> ("LastName ")! = Null
Select employee;
This article reposted: http://www.cnblogs.com/SkySoot/archive/2012/08/21/2649471.html is a good example, tell you that the data set dataset can not be directly operated by the linq to the first operation to convert to the able and the implementation of the extension method AsEnumerable () can, note that the returned object set cannot be directly bound to the GridView.

LINQ (LINQ to DataSet)

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.