|
LINQ to DataSet主要是提供對離線數據的支持,只有在填充DataSet之后,我們才能使用LINQ to DataSet來查詢數據。其功能主要是通過System.Data.DataRowExtions和System.Data.DataTableExtensions兩個靜態類中的擴展方法來公開的。LINQ to DataSet是LINQ to ADO.NET中的一部分,但這部分所占比重非常小,內容也比較少。下面就讓我們首先來看看DataTableExtensions中的擴展方法:
public static EnumerableRowCollection<DataRow> AsEnumerable(this DataTable source)----------------------------------------------------------------------------------------------------
public static DataView AsDataView(this DataTable table)
public static DataView AsDataView<T>(this EnumerableRowCollection<T> source) where T : DataRow
-----------------------------------------------------------------
public static DataTable CopyToDataTable<T>(this IEnumerable<T> source) where T : DataRow
public static void CopyToDataTable<T>(this IEnumerable<T> source,DataTable table,LoadOption options) where T : DataRow
public static void CopyToDataTable<T>(this IEnumerable<T> source,DataTable table,LoadOption options,FillErrorEventHandler errorHandler) where T : DataRow
下面,我們就來看看一個使用LINQ to DataSet的實例,這個例子主要描述了一下上面擴展方法的用法,同時給出了部分注意的事項:































































































這個例子的輸出結果為:
We try to get access to original version, so we will get the exception.:================================================================
Exception:There is no Original data to access.
================================================================
We will use CopyToDataTable to build original version.
================================================================
Student Id = 1 :original Lazy Bee:current Lazy Bee
Student Id = 7 :original Flying Wind:current Flying Wind
Student Id = 13 :original PiPi Zhu:current PiPi Zhu
Student Id = 72 :original Chong Chong:current Chong Chong
================================================================
After call SetField to change name.
================================================================
Student Id = 1 :original Lazy Bee:current Lazy Bee
Student Id = 7 :original Flying Wind:current Flying Wind
Student Id = 13 :original PiPi Zhu:current George Oscar Bluth
Student Id = 72 :original Chong Chong:current Chong Chong
================================================================
After call SetField to change name to null.
================================================================
Student Id = 1 :original Lazy Bee:current Lazy Bee
Student Id = 7 :original Flying Wind:current Flying Wind
Student Id = 13 :original PiPi Zhu:current
Student Id = 72 :original Chong Chong:current Chong Chong
================================================================
After call CopyToDataTable.We will not get our expected result because we have not set primary key.
================================================================
Student Id = 1 :original Lazy Bee:current Lazy Bee
Student Id = 7 :original Flying Wind:current Flying Wind
Student Id = 13 :original PiPi Zhu:current PiPi Zhu
Student Id = 72 :original Chong Chong:current Chong Chong
Student Id = 1 :original Lazy Bee:current Lazy Bee
Student Id = 7 :original Flying Wind:current Flying Wind
Student Id = 13 :original :current
Student Id = 72 :original Chong Chong:current Chong Chong
================================================================
After call Distinct.We will not get our expected result because we have not used DatarowComparer.Default comparer.
================================================================
Student Id = 1 :original Lazy Bee:current Lazy Bee
Student Id = 7 :original Flying Wind:current Flying Wind
Student Id = 13 :original PiPi Zhu:current PiPi Zhu
Student Id = 72 :original Chong Chong:current Chong Chong
Student Id = 1 :original Lazy Bee:current Lazy Bee
Student Id = 7 :original Flying Wind:current Flying Wind
Student Id = 13 :original :current
Student Id = 72 :original Chong Chong:current Chong Chong
================================================================
After call Distinct.this is what we want.
================================================================
Student Id = 1 :original Lazy Bee:current Lazy Bee
Student Id = 7 :original Flying Wind:current Flying Wind
Student Id = 13 :original PiPi Zhu:current PiPi Zhu
Student Id = 72 :original Chong Chong:current Chong Chong
Student Id = 13 :original :current
================================================================
After call CopyToDataTable.this is what we want.
================================================================
Student Id = 1 :original Lazy Bee:current Lazy Bee
Student Id = 7 :original Flying Wind:current Flying Wind
Student Id = 13 :original :current
Student Id = 72 :original Chong Chong:current Chong Chong
================================================================
it知識庫:LINQ To DataSet,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。