Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement query by LINQ to DataSet

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to achieve LINQ to DataSet query, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

LINQ to DataSet mainly provides support for offline data, and only after we populate DataSet can we use LINQ to DataSet to query data. Its function is mainly exposed through extension methods in two static classes, System.Data.DataRowExtions and System.Data.DataTableExtensions. LINQ to DataSet is a part of LINQ to ADO.Net, but the proportion of this part is very small and the content is relatively small.

Let's first look at the extension methods in DataTableExtensions:

Public static EnumerableRowCollection

AsEnumerable (this DataTable source)

Public static DataView AsDataView (this DataTable table)

Public static DataView AsDataView

(this EnumerableRowCollection source) where T: DataRow

Public static DataTable CopyToDataTable

(this IEnumerable source) where T: DataRow

Public static void CopyToDataTable (this IEnumerable source

DataTable table,LoadOption options) where T: DataRow

Public static void CopyToDataTable (this IEnumerable source

DataTable table,LoadOption options,FillErrorEventHandler errorHandler)

Where T: DataRow

It can be seen from the definition that these three categories mainly provide the conversion between DataTable, DataView and IEnumerable. As we all know, LINQ to Object queries are mainly operations on IEnumerable sequences, which establishes a conversion bridge between DataTable, DataView and LINQ.

Therefore, before we need to apply DataTable to LINQ to DataSet queries, we need to call AsEnumerable to complete the conversion from DataTable to LINQ. If we need to data-bind the results of a LINQ to DataSet query, we need to call the generic version of AsDataView to complete the conversion from LINQ to DataView. Of course, we can also use CopyToDataTable for LINQ-to-DataTable conversion.

Note: after we have completed the conversion from DataTable to LINQ (IEnumerable) (that is, calling the AsEnumerable extension method), we need to perform a collection operation of two DataRow sequences, such as Distinct,Union,Except,Intersect,SequenceEqual, which need to compare the equality of the elements in the data source. Because by default, we call the GetHashCode and Equals operations of the elements in the data source to determine whether the references of the objects are equal. This may lead to unexpected results (the data content in DataRow is the same, but not the same object), so we will use the overloaded version of Distinct,Union,Except,Intersect,SequenceEqual with IEqualityComparer, using System.Data.DataRowComparer.Default as a parameter. This comparator class is new to .net 3.5 specifically for LINQ to DataSet and is used to compare the value of DataRow by comparing the number of DataColumn first and then using the Equals method of the type in the column.

The CopyToDataTable method without the LoadOptions parameter automatically creates (updates) the original version and the current version for each field in each row, and the overloaded version of CopyToDataTable with the LoadOptions parameter allows you to specify whether to create (update) the original version or the current version, or both. The LoadOptions option has the following three option values to choose from:

◆ OverwriteChanges: create (update) the current and original values for each column

◆ PreserveChanges: create (update) the original values for each column

◆ Upset: create (update) the current value of each column

Thank you for reading this article carefully. I hope the article "how to query LINQ to DataSet" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report