|
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,转载需保留来源!
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。