在我们实际编程的工程中,常常遇到这样的情况:DataTable并不是数据库中的,或者DataTable尚未写到数据库,或者从数据库中读出的DataTable已经在本地被改动,又没有写回数据库(可能还要作其他改动),在这些情况下,其实只要用.NET类库中提供的DataView类的强大功能(主要是用它的RowFilter属性),就能方便地解决这类查询问题。
常有网友在网上询问怎么在DataTable中执行DataTable.Select("条件")返回DataTable,今天我在这里给个解决方法给大家参考:代码如下
/// <summary>
/// 执行DataTable中的查询返回新的DataTable
/// </summary>
/// <param name="dt">源数据DataTable</param>
/// <param name="condition">查询条件</param>
/// <returns></returns>
private DataTable GetNewDataTable(DataTable dt,string condition)
{
DataTable newdt = new DataTable();
newdt=dt.Clone();
DataRow[] dr = dt.Select(condition);
for(int i=0;i<dr.Length;i++)
{
newdt.ImportRow((DataRow)dr[i]);
}
return newdt;//返回的查询结果
}
--
网友PS:
循环添加行结果也一样:
DataRow[] rows = dt.Select(conditions);
foreach(DataRow row in rows)
{
newdt.Rows.Add(row.ItemArray);
}
return newdt;
DataTable.Select方法返回获取所有DataRow对象的数组。Select()有四个重载方法.
名称 | 说明 | |
---|---|---|
Select () | 获取所有 DataRow 对象的数组。 | |
Select(String) | 按照主键顺序(如果没有主键,则按照添加顺序)获取与筛选条件相匹配的所有 DataRow 对象的数组。 | |
Select(String, String) | 获取按照指定的排序顺序且与筛选条件相匹配的所有 DataRow 对象的数组。 | |
Select(String, String, DataViewRowState) | 获取与排序顺序中的筛选器以及指定的状态相匹配的所有 DataRow 对象的数组。 |
1.Select()示例:
private void GetRows()
{
// Get the DataTable of a DataSet.
DataTable table = DataSet1.Tables["Suppliers"];
DataRow[] rows = table.Select();
// Print the value one column of each DataRow.
for(int i = 0; i < rows.Length ; i++)
{
Console.WriteLine(rows[i]["CompanyName"]);
}
}
2.Select(string filterExpression)示例:
private void GetRowsByFilter()
{
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "Date > #1/1/00#";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}
3.Select(string filterExpression,string sort)示例:
private void GetRowsByFilter()
{
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression = "Date > '1/1/00'";
// Sort descending by column named CompanyName.
string sortOrder = "CompanyName DESC";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression, sortOrder);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}
4.Select(string filterExpression,string sort, DataViewRowState recordStates)示例:
private static void GetRowsByFilter()
{
DataTable customerTable = new DataTable("Customers");
// Add columns
customerTable.Columns.Add("id", typeof(int));
customerTable.Columns.Add("name", typeof(string));
// Set PrimaryKey
customerTable.Columns[ "id" ].Unique = true;
customerTable.PrimaryKey = new DataColumn[]
{ customerTable.Columns["id"] };
// Add ten rows
for(int id=1; id<=10; id++)
{
customerTable.Rows.Add(
new object[] { id, string.Format("customer{0}", id) });
}
customerTable.AcceptChanges();
// Add another ten rows
for(int id=11; id<=20; id++)
{
customerTable.Rows.Add( new object[] { id, string.Format("customer{0}", id) });
}
string expression;
string sortOrder;
DataTable.Select方法返回获取所有DataRow对象的数组。Select()有四个重载方法.
名称 | 说明 | |
---|---|---|
Select () | 获取所有 DataRow 对象的数组。 | |
Select(String) | 按照主键顺序(如果没有主键,则按照添加顺序)获取与筛选条件相匹配的所有 DataRow 对象的数组。 | |
Select(String, String) | 获取按照指定的排序顺序且与筛选条件相匹配的所有 DataRow 对象的数组。 | |
Select(String, String, DataViewRowState) | 获取与排序顺序中的筛选器以及指定的状态相匹配的所有 DataRow 对象的数组。 |
1.Select()示例:
private void GetRows()
{
// Get the DataTable of a DataSet.
DataTable table = DataSet1.Tables["Suppliers"];
DataRow[] rows = table.Select();
// Print the value one column of each DataRow.
for(int i = 0; i < rows.Length ; i++)
{
Console.WriteLine(rows[i]["CompanyName"]);
}
}
2.Select(string filterExpression)示例:
private void GetRowsByFilter()
{
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "Date > #1/1/00#";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}
3.Select(string filterExpression,string sort)示例:
private void GetRowsByFilter()
{
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression = "Date > '1/1/00'";
// Sort descending by column named CompanyName.
string sortOrder = "CompanyName DESC";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression, sortOrder);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}
4.Select(string filterExpression,string sort, DataViewRowState recordStates)示例:
private static void GetRowsByFilter()
{
DataTable customerTable = new DataTable("Customers");
// Add columns
customerTable.Columns.Add("id", typeof(int));
customerTable.Columns.Add("name", typeof(string));
// Set PrimaryKey
customerTable.Columns[ "id" ].Unique = true;
customerTable.PrimaryKey = new DataColumn[]
{ customerTable.Columns["id"] };
// Add ten rows
for(int id=1; id<=10; id++)
{
customerTable.Rows.Add(
new object[] { id, string.Format("customer{0}", id) });
}
customerTable.AcceptChanges();
// Add another ten rows
for(int id=11; id<=20; id++)
{
customerTable.Rows.Add( new object[] { id, string.Format("customer{0}", id) });
}
string expression;
string sortOrder;
expression = "id > 5";
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述