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 包含指定的列,其顺序与这些列在该数组中的顺序相同。

 

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

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Data;
  6 
  7 namespace TableTest
  8 {
  9     class Program
 10     {
 11         static void Main(string[] args)
 12         {
 13             DemonstrateDataView();
 14         }
 15 
 16         private static void DemonstrateDataView()
 17         {
 18             // Create a DataTable with three columns.
 19             DataTable table = new DataTable("NewTable");
 20             Console.WriteLine("Original table name: " + table.TableName);
 21             #region Columns
 22             DataColumn column = new DataColumn("ID", typeof(System.Int32));
 23             table.Columns.Add(column);
 24 
 25             column = new DataColumn("Category", typeof(System.String));
 26             table.Columns.Add(column);
 27 
 28             column = new DataColumn("Product", typeof(System.String));
 29             table.Columns.Add(column);
 30 
 31             column = new DataColumn("QuantityInStock", typeof(System.Int32));
 32             table.Columns.Add(column);
 33             #endregion
 34 
 35             #region Rows
 36             // Add some items.
 37             DataRow row = table.NewRow();
 38             row.ItemArray = new object[] { 1, "Fruit", "Apple", 14 };
 39             table.Rows.Add(row);
 40 
 41             row = table.NewRow();
 42             row.ItemArray = new object[] { 1, "Fruit", "Apple", 14 };
 43             table.Rows.Add(row);
 44 
 45             row = table.NewRow();
 46             row.ItemArray = new object[] { 2, "Fruit", "Orange", 27 };
 47             table.Rows.Add(row);
 48 
 49             row = table.NewRow();
 50             row.ItemArray = new object[] { 3, "Bread", "Muffin", 23 };
 51             table.Rows.Add(row);
 52 
 53             row = table.NewRow();
 54             row.ItemArray = new object[] { 4, "Fish", "Salmon", 12 };
 55             table.Rows.Add(row);
 56 
 57             row = table.NewRow();
 58             row.ItemArray = new object[] { 5, "Fish", "Salmon", 15 };
 59             table.Rows.Add(row);
 60 
 61             row = table.NewRow();
 62             row.ItemArray = new object[] { 6, "Bread", "Croissant", 23 };
 63             table.Rows.Add(row);
 64             #endregion
 65 
 66             // Mark all rows as "accepted". Not required
 67             // for this particular example.
 68             table.AcceptChanges();
 69 
 70             // Print current table values.
 71             PrintTableOrView(table, "Current Values in Table");
 72 
 73             DataView view = new DataView(table);
 74             view.Sort = "Category";
 75             PrintTableOrView(view, "Current Values in View");
 76 
 77             DataTable newTable = view.ToTable(true, "ID", "Category", "Product", "QuantityInStock");
 78             PrintTableOrView(newTable, "Table created from sorted DataView");
 79             Console.WriteLine("New table name: " + newTable.TableName);
 80 
 81             Console.WriteLine("Press any key to continue.");
 82             Console.ReadKey();
 83         }
 84 
 85         private static void PrintTableOrView(DataView dv, string label)
 86         {
 87             System.IO.StringWriter sw;
 88             string output;
 89             DataTable table = dv.Table;
 90 
 91             Console.WriteLine(label);
 92 
 93             // Loop through each row in the view.
 94             foreach (DataRowView rowView in dv)
 95             {
 96                 sw = new System.IO.StringWriter();
 97 
 98                 // Loop through each column.
 99                 foreach (DataColumn col in table.Columns)
100                 {
101                     // Output the value of each column's data.
102                     sw.Write(rowView[col.ColumnName].ToString() + ", ");
103                 }
104                 output = sw.ToString();
105                 // Trim off the trailing ", ", so the output looks correct.
106                 if (output.Length > 2)
107                 {
108                     output = output.Substring(0, output.Length - 2);
109                 }
110                 // Display the row in the console window.
111                 Console.WriteLine(output);
112             }
113             Console.WriteLine();
114         }
115 
116 
117         private static void PrintTableOrView(DataTable table, string label)
118         {
119             System.IO.StringWriter sw;
120             string output;
121 
122             Console.WriteLine(label);
123 
124             // Loop through each row in the table.
125             foreach (DataRow row in table.Rows)
126             {
127                 sw = new System.IO.StringWriter();
128                 // Loop through each column.
129                 foreach (DataColumn col in table.Columns)
130                 {
131                     // Output the value of each column's data.
132                     sw.Write(row[col].ToString() + ", ");
133                 }
134                 output = sw.ToString();
135                 // Trim off the trailing ", ", so the output looks correct.
136                 if (output.Length > 2)
137                 {
138                     output = output.Substring(0, output.Length - 2);
139                 }
140                 // Display the row in the console window.
141                 Console.WriteLine(output);
142             } //
143             Console.WriteLine();
144         }
145     }
146 }
View Code

 

好了。。。文章完了。。如果大家还有好的方法,欢迎介绍啊。据说还有一个使用sql中的rownumber方法实现的distinct功能,有知道的介绍个呗 O(∩_∩)O哈哈~

posted @ 2013-11-09 15:45  for.life  阅读(1181)  评论(0)    收藏  举报