to:kuangsha007(狂晕) 谢谢你了。 的确这样可以得到类似于 select * from data where id > 5 order by name;的结果 但是我想要的是类似于 select name ,sum(money) from data group by name;的结果也就是得到指定的列,并分类汇总。DataTable能实现吗?
private void ComputeBySalesSalesID(DataSet myDataSet){ // Presumes a DataTable named "Orders" that has a column named "Total." DataTable myTable; myTable = myDataSet.Tables["Orders"]; // Declare an object variable. object objSum; objSum = myTable.Compute("Sum(Total)", "EmpID = 5"); }
老大,我就是不知道,Select(string, string, DataViewRowState)里的三个参数怎么来填,它返回的是DataRow[],dataRow里只有我想要的几列吗。
{
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 strExpr;
string strSort;
strExpr = "id > 5";
// Sort descending by column named CompanyName.
strSort = "name DESC";
// Use the Select method to find all rows matching the filter.
DataRow[] foundRows =
customerTable.Select( strExpr, strSort, DataViewRowState.Added );
PrintRows( foundRows, "filtered rows" ); foundRows = customerTable.Select();
PrintRows( foundRows, "all rows" );
}
谢谢你了。
的确这样可以得到类似于 select * from data where id > 5 order by name;的结果
但是我想要的是类似于 select name ,sum(money) from data group by name;的结果也就是得到指定的列,并分类汇总。DataTable能实现吗?
不知道这样对不对。Decimal money ;
String name;
foreach( DataRow dr in DataTable.Rows )
{
name = convert.ToString( dr["name"] );
foreach( DataRow datarow in DataTable.Rows )
{
String text = convert.ToString( datarow["name"] );
if( text == name )
{
money += convert.ToDecimal( dr["money"] );
}
}这只是个思路
谢谢你的提供的思路,很有用呢。
我就是想要实现, 在程序中 不用数据库,真接在内存中创建一个类似于表的东东,
然后,再对这个表可以像对数据库一样来操作,并得到结果集。
主要是希望能对它进行分类查询的操作,有sum(), min() max() avg() count()
在Expression表达式中支持下列聚合类型:Sum(求和)Avg(平均)Min(最小值)Max(最大值)Count(计数)StDev(统计标准偏差)Var(统计方差)。使用DataTable的Select方法好像无法实现选择某些列进行输入,最好用DataView
获取与排序顺序中的筛选器以及指定的状态相匹配的所有 DataRow 对象的数组。
public DataRow[] Select(string filterExpression,string sort,DataViewRowState recordStates);
参数
filterExpression
要用来筛选行的条件。
sort
一个字符串,它指定列和排序方向。
recordStates
DataViewRowState 值之一。
dataset.Table[0].Compute();
~~~~
你可看看Msdn,
这个应该可以实现你要得
dataset.Table[0].Compute();
~~~~
你可看看Msdn,
这个应该可以实现你要得
// Presumes a DataTable named "Orders" that has a column named "Total."
DataTable myTable;
myTable = myDataSet.Tables["Orders"];
// Declare an object variable.
object objSum;
objSum = myTable.Compute("Sum(Total)", "EmpID = 5");
}