C#程序中使用DataView的ToTable方法实现distinct功能

在大数据量查询中经常会碰到使用distinct来获取所有不同的数据,然后当数据量达到一定数量时,distinct会是查询效率降低,是查询时间集中在排序区分上面。导致查询缓慢。如果将distinct这部分功能放在代码中则会是查询效率提升。同时将distinct这部分的在程序中来解决的话则可以极大的缓解数据库的压力,而程序部分的则很容易提升。相对来说。数据库的资源比其他资源要宝贵的多。好了。前话到这里。

要在程序中实现distinct功能的话。写代码是必须的,当然相对来说是如何写比较简单的问题。在这方面,微软给我们提供了贴心的服务在DataView 中有一个ToTable方法。这个方法有四个重载方法,这里直说第三种重载方法。

DataView.ToTable 方法 (Boolean, String[])

这个方法:

根据现有 DataView 中的行,创建并返回一个新的 DataTable

命名空间:System.Data
程序集:System.Data(在 system.data.dll 中)

注意:此方法在 .NET Framework 2.0 版中是新增的。

该方法原型是介个样子的:public DataTable ToTable(bool distinct, params string[] columnNames);

这里的distinct是一个bool类型。如果指明为true则表示按照后面可变参数中的列名来实现获取这些列中的distinct数据。如下:

 

参数

 

distinct

如果为 true,则返回的 DataTable 将包含所有列都具有不同值的行。默认值为 false

 

columnNames

一个字符串数组,包含要包括在返回的 DataTable 中的列名的列表。DataTable 包含指定的列,其顺序与这些列在该数组中的顺序相同。

 

最后我们以一个小的例子来结尾。上代码~\(≧▽≦)/~啦啦啦!!!!!

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data;  namespace TableTest {     class Program     {         static void Main(string[] args)         {             DemonstrateDataView();         }          private static void DemonstrateDataView()         {             // Create a DataTable with three columns.             DataTable table = new DataTable("NewTable");             Console.WriteLine("Original table name: " + table.TableName);             #region Columns             DataColumn column = new DataColumn("ID", typeof(System.Int32));             table.Columns.Add(column);              column = new DataColumn("Category", typeof(System.String));             table.Columns.Add(column);              column = new DataColumn("Product", typeof(System.String));             table.Columns.Add(column);              column = new DataColumn("QuantityInStock", typeof(System.Int32));             table.Columns.Add(column);             #endregion              #region Rows             // Add some items.             DataRow row = table.NewRow();             row.ItemArray = new object[] { 1, "Fruit", "Apple", 14 };             table.Rows.Add(row);              row = table.NewRow();             row.ItemArray = new object[] { 1, "Fruit", "Apple", 14 };             table.Rows.Add(row);              row = table.NewRow();             row.ItemArray = new object[] { 2, "Fruit", "Orange", 27 };             table.Rows.Add(row);              row = table.NewRow();             row.ItemArray = new object[] { 3, "Bread", "Muffin", 23 };             table.Rows.Add(row);              row = table.NewRow();             row.ItemArray = new object[] { 4, "Fish", "Salmon", 12 };             table.Rows.Add(row);              row = table.NewRow();             row.ItemArray = new object[] { 5, "Fish", "Salmon", 15 };             table.Rows.Add(row);              row = table.NewRow();             row.ItemArray = new object[] { 6, "Bread", "Croissant", 23 };             table.Rows.Add(row);             #endregion              // Mark all rows as "accepted". Not required             // for this particular example.             table.AcceptChanges();              // Print current table values.             PrintTableOrView(table, "Current Values in Table");              DataView view = new DataView(table);             view.Sort = "Category";             PrintTableOrView(view, "Current Values in View");              DataTable newTable = view.ToTable(true, "ID", "Category", "Product", "QuantityInStock");             PrintTableOrView(newTable, "Table created from sorted DataView");             Console.WriteLine("New table name: " + newTable.TableName);              Console.WriteLine("Press any key to continue.");             Console.ReadKey();         }          private static void PrintTableOrView(DataView dv, string label)         {             System.IO.StringWriter sw;             string output;             DataTable table = dv.Table;              Console.WriteLine(label);              // Loop through each row in the view.             foreach (DataRowView rowView in dv)             {                 sw = new System.IO.StringWriter();                  // Loop through each column.                 foreach (DataColumn col in table.Columns)                 {                     // Output the value of each column's data.                     sw.Write(rowView[col.ColumnName].ToString() + ", ");                 }                 output = sw.ToString();                 // Trim off the trailing ", ", so the output looks correct.                 if (output.Length > 2)                 {                     output = output.Substring(0, output.Length - 2);                 }                 // Display the row in the console window.                 Console.WriteLine(output);             }             Console.WriteLine();         }           private static void PrintTableOrView(DataTable table, string label)         {             System.IO.StringWriter sw;             string output;              Console.WriteLine(label);              // Loop through each row in the table.             foreach (DataRow row in table.Rows)             {                 sw = new System.IO.StringWriter();                 // Loop through each column.                 foreach (DataColumn col in table.Columns)                 {                     // Output the value of each column's data.                     sw.Write(row[col].ToString() + ", ");                 }                 output = sw.ToString();                 // Trim off the trailing ", ", so the output looks correct.                 if (output.Length > 2)                 {                     output = output.Substring(0, output.Length - 2);                 }                 // Display the row in the console window.                 Console.WriteLine(output);             } //             Console.WriteLine();         }     } }