LINQ To DataSet
这个例子的输出结果为:
================================================================
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
================================================================
总结,在使用LINQ to DataSet的时候需要注意以下几个方面:
1、 在对IEnumeralbe进行数据行的集合操作如Distinct, Except, Union, Intersect, SequenceEqual时,需要使用System.Data.DatarowComparer.Default作为比较器作为输入参数,以保证对DataRow是进行值比较,而不是引用比较。当然,如果GroupBy或者其他操作的key的类型是DataRow时,也需要使用此比较器,以使我们得到我们期望的行为。
2 、SetField可以将字段值设置为null,并且SetField方法将自动将其转换为DBNull.Value.
3 、Field可以完成从DBNull.Value到null的转换。也就是说,如果该字段的值是DBNull.Value 时,Field方法将自动将其转为null并返回。这个方法是强类型的,不需要象通过列名或者列索引返回字段值一样将Object类型进行造型成需要的类型(值类型进行拆箱操作),(如果字段的值是DBNull.Value时进行造型还将导致抛出异常)Field扩展方法自动做了这些处理,省去了开发人员手动进行这些处理的麻烦。
4 、缺省情况下,数据行的Original版本中是没有值的,试图访问时将导致异常发生。当然, 可以在访问之前使用DataRow.HasVersion来进行判断,以防止异常的发生。也可以通过调用DataRow.AcceptChanges方法来建立Original版本来避免异常的发生。不带LoadOptions参数的CopyToDataTable扩展方法也会为返回的DataTable自动建立DataRow的Original和Current版本.
5 当使用带LoadOptions输入参数的CopyToDataTable扩展方法时,必须为目标DataTable指定主键列,否则,该函数只是将源DataTable追加到目标DataTable的最后面。可能达不到期望更新的结果。