LINQ To DataSet
下面,我们就来看看一个使用LINQ to DataSet的实例,这个例子主要描述了一下上面扩展方法的用法,同时给出了部分注意的事项:
public static class LINQToDataSet
{
public class Student
{
public int Id;
public string Name;
}
public static DataTable GetDataTable(Student[] students)
{
DataTable table = new DataTable();
table.Columns.Add("Id", typeof(Int32));
table.Columns.Add("Name", typeof(string));
foreach (Student student in students)
{
table.Rows.Add(student.Id, student.Name);
}
return (table);
}
public static void PrintStudentTable(DataTable dt)
{
PrintStudentTable(dt.AsEnumerable());
}
public static void PrintStudentTable(IEnumerable<DataRow> dt)
{
Console.WriteLine("================================================================");
try
{
foreach (DataRow dataRow in dt.AsEnumerable())
{
Console.WriteLine("Student Id = {0} :original {1}:current {2}",
dataRow.Field<int>("Id"),
dataRow.Field<string>("Name", DataRowVersion.Original),
dataRow.Field<string>("Name", DataRowVersion.Current));
}
}
catch (Exception e)
{
Console.WriteLine("Exception:" + e.Message);
}
Console.WriteLine("================================================================");
}
public static void Test()
{
Student[] students = { new Student { Id = 1, Name = "Lazy Bee" },
new Student { Id = 7, Name = "Flying Wind" },
new Student { Id = 13, Name = "PiPi Zhu" },
new Student { Id = 72, Name = "Chong Chong" }};
DataTable originalTable = GetDataTable(students);
//我们试图访问DataTable中数据行的Orginal版本,由于此时还没有建立原始版本,
//所以将导致异常
Console.WriteLine("We try to get access to original version, so we will get the exception.:");
PrintStudentTable(originalTable);
//我们使用CopyToDataTable来建立DataTable中数据行的Orginal版本
Console.WriteLine("We will use CopyToDataTable to build original version.");
DataTable newTable = originalTable.AsEnumerable().CopyToDataTable();
PrintStudentTable(newTable);
//使用SetField来更新
Console.WriteLine("After call SetField to change name.");
(from s in newTable.AsEnumerable()
where s.Field<string>("Name") == "PiPi Zhu"
select s).Single<DataRow>().SetField("Name", "George Oscar Bluth");
PrintStudentTable(newTable);
//使用SetField来设置null
Console.WriteLine("After call SetField to change name to null.");
(from s in newTable.AsEnumerable()
where s.Field<int>("Id") == 13
select s).Single<DataRow>().SetField<string>("Name", null);
PrintStudentTable(newTable);
//使用CopyToDataTable来合并,由于我们没有指定表的主键,
//所以只会简单的追加在目标数据表的最后
Console.WriteLine("After call CopyToDataTable.We will not get our expected result because we have not set primary key.");
//首先,我们调用AcceptChanges来建立Original版本,否则,避免显示时抛出异常
originalTable.AcceptChanges();
newTable.AsEnumerable().CopyToDataTable(originalTable, LoadOption.OverwriteChanges);
PrintStudentTable(originalTable);
//我们使用Distinct来去掉刚才重复的记录,由于此时我们没有使用DatarowComparer.Default
//所以我们将得不到我们想要的结果
Console.WriteLine("After call Distinct.We will not get our expected result because we have not used DatarowComparer.Default comparer.");
IEnumerable<DataRow> distinctTable=originalTable.AsEnumerable().Distinct();
PrintStudentTable(distinctTable);
//我们使用Distinct来去掉刚才重复的记录,使用DatarowComparer.Default比较器
//所以我们将得到我们想要的结果
Console.WriteLine("After call Distinct.this is what we want.");
distinctTable=originalTable.AsEnumerable().Distinct(DataRowComparer.Default);
PrintStudentTable(distinctTable);
//我们先设置主键,然后再使用CopyToDataTable来合并
Console.WriteLine("After call CopyToDataTable.this is what we want.");
originalTable = GetDataTable(students);
originalTable.PrimaryKey = new DataColumn[] { originalTable.Columns["Id"] };
newTable.AsEnumerable().CopyToDataTable(originalTable, LoadOption.OverwriteChanges);
PrintStudentTable(originalTable);
}
}
{
public class Student
{
public int Id;
public string Name;
}
public static DataTable GetDataTable(Student[] students)
{
DataTable table = new DataTable();
table.Columns.Add("Id", typeof(Int32));
table.Columns.Add("Name", typeof(string));
foreach (Student student in students)
{
table.Rows.Add(student.Id, student.Name);
}
return (table);
}
public static void PrintStudentTable(DataTable dt)
{
PrintStudentTable(dt.AsEnumerable());
}
public static void PrintStudentTable(IEnumerable<DataRow> dt)
{
Console.WriteLine("================================================================");
try
{
foreach (DataRow dataRow in dt.AsEnumerable())
{
Console.WriteLine("Student Id = {0} :original {1}:current {2}",
dataRow.Field<int>("Id"),
dataRow.Field<string>("Name", DataRowVersion.Original),
dataRow.Field<string>("Name", DataRowVersion.Current));
}
}
catch (Exception e)
{
Console.WriteLine("Exception:" + e.Message);
}
Console.WriteLine("================================================================");
}
public static void Test()
{
Student[] students = { new Student { Id = 1, Name = "Lazy Bee" },
new Student { Id = 7, Name = "Flying Wind" },
new Student { Id = 13, Name = "PiPi Zhu" },
new Student { Id = 72, Name = "Chong Chong" }};
DataTable originalTable = GetDataTable(students);
//我们试图访问DataTable中数据行的Orginal版本,由于此时还没有建立原始版本,
//所以将导致异常
Console.WriteLine("We try to get access to original version, so we will get the exception.:");
PrintStudentTable(originalTable);
//我们使用CopyToDataTable来建立DataTable中数据行的Orginal版本
Console.WriteLine("We will use CopyToDataTable to build original version.");
DataTable newTable = originalTable.AsEnumerable().CopyToDataTable();
PrintStudentTable(newTable);
//使用SetField来更新
Console.WriteLine("After call SetField to change name.");
(from s in newTable.AsEnumerable()
where s.Field<string>("Name") == "PiPi Zhu"
select s).Single<DataRow>().SetField("Name", "George Oscar Bluth");
PrintStudentTable(newTable);
//使用SetField来设置null
Console.WriteLine("After call SetField to change name to null.");
(from s in newTable.AsEnumerable()
where s.Field<int>("Id") == 13
select s).Single<DataRow>().SetField<string>("Name", null);
PrintStudentTable(newTable);
//使用CopyToDataTable来合并,由于我们没有指定表的主键,
//所以只会简单的追加在目标数据表的最后
Console.WriteLine("After call CopyToDataTable.We will not get our expected result because we have not set primary key.");
//首先,我们调用AcceptChanges来建立Original版本,否则,避免显示时抛出异常
originalTable.AcceptChanges();
newTable.AsEnumerable().CopyToDataTable(originalTable, LoadOption.OverwriteChanges);
PrintStudentTable(originalTable);
//我们使用Distinct来去掉刚才重复的记录,由于此时我们没有使用DatarowComparer.Default
//所以我们将得不到我们想要的结果
Console.WriteLine("After call Distinct.We will not get our expected result because we have not used DatarowComparer.Default comparer.");
IEnumerable<DataRow> distinctTable=originalTable.AsEnumerable().Distinct();
PrintStudentTable(distinctTable);
//我们使用Distinct来去掉刚才重复的记录,使用DatarowComparer.Default比较器
//所以我们将得到我们想要的结果
Console.WriteLine("After call Distinct.this is what we want.");
distinctTable=originalTable.AsEnumerable().Distinct(DataRowComparer.Default);
PrintStudentTable(distinctTable);
//我们先设置主键,然后再使用CopyToDataTable来合并
Console.WriteLine("After call CopyToDataTable.this is what we want.");
originalTable = GetDataTable(students);
originalTable.PrimaryKey = new DataColumn[] { originalTable.Columns["Id"] };
newTable.AsEnumerable().CopyToDataTable(originalTable, LoadOption.OverwriteChanges);
PrintStudentTable(originalTable);
}
}
例子中有比较详尽的注释,相信大家看应该没有什么问题。