datatable查询问题

myloveangel 2007-02-02 11:13:26
我想在datatable中而不是在数据库表中查询,要去除重复值应该怎么做呢??
...全文
415 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuqunying0545 2008-12-20
  • 打赏
  • 举报
回复
这应该是逐条绑定数据吧,本姑娘做过!要用到临时的datatable!
tonyhawk 2007-02-03
  • 打赏
  • 举报
回复
mark
marvelstack 2007-02-03
  • 打赏
  • 举报
回复
在.net 2.0中增加了
DataView.ToTable 方法可以处理,,参看
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref4/html/M_System_Data_DataView_ToTable_1_ae7c231f.htm

private static void DemonstrateDataView()
{
// Create a DataTable with three columns.
DataTable table = new DataTable("NewTable");
Console.WriteLine("Original table name: " + table.TableName);
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);

// 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[] { 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);

// 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, "Category", "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();
}

该示例在控制台窗口中显示以下输出:

Original table name: NewTable
Current Values in Table
1, Fruit, Apple, 14
2, Fruit, Orange, 27
3, Bread, Muffin, 23
4, Fish, Salmon, 12
5, Fish, Salmon, 15
6, Bread, Croissant, 23

Current Values in View
3, Bread, Muffin, 23
6, Bread, Croissant, 23
4, Fish, Salmon, 12
5, Fish, Salmon, 15
1, Fruit, Apple, 14
2, Fruit, Orange, 27

Table created from sorted DataView
Bread, 23
Fish, 12
Fish, 15
Fruit, 14
Fruit, 27

New table name: NewTable
vengair 2007-02-02
  • 打赏
  • 举报
回复
先按你要求选出来 在一个 DataRowCollection 里
然后遍历
把选出的行中你定义不能重复的列的值写在一个 List 里面
每次遍历是检查一下 List 里面是否已有
如果有了,就把当前行从 DataRowCollection 里删除
另外注意,因为是一边删一边编历
用 for 的时候从索引最大的开始 直到 0
避免索引超出范围
yoursunboy 2007-02-02
  • 打赏
  • 举报
回复
数据不多的话,递归遍历比较爽 ~

==================================
小小姑娘
清早起床
提着裤子上茅房
茅房有人,没有办法
只好拉在裤子上..................
QQ:18163708;765835
MSN:yoursunboy@msn.com
Gtalk:yoursunboy@gmail.com
==================================
myloveangel 2007-02-02
  • 打赏
  • 举报
回复
啊,谢谢好人

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧