我有一个 DataSet dsInvoiceData,其中包含字段" ACTIVITY"," LOGUID"," VALUE"。
问题数据集的示例:
ACTIVITY-----LOGUID-----VALUE
MOVE ------- AAAAAA ----- 000000
STORE ------- AAAAAA ----- 111111
STORE ------- AAAAAA ----- 222222
STORE ------- AAAAAA ----- 333333
MOVE ------- BBBBBB ----- 000000
STORE ------- BBBBBB ----- 222222
如何创建新的DATASET(来自当前版本)?
只包含行:
(WHERE VALUE = 000000)
OR
(IF (VALUE != 000000) AND (each LOGUID can only have DISTINCT ACTIVITY))
示例,数据集应为:
ACTIVITY-----LOGUID-----VALUE
MOVE ------- AAAAAA ----- 000000
STORE ------- AAAAAA ----- 111111
MOVE ------- BBBBBB ----- 000000
STORE ------- BBBBBB ----- 222222
答案 0 :(得分:1)
您应该使用DefaultView属性,并过滤数据表
var t = new DataSet();
t.Tables[0].DefaultView.RowFilter = "VALUE = 000000";
t.Tables[0].DefaultView.ToTable();
ToTable()
方法用于访问过滤表的结果。
您可以在此链接中探索可与RowFilter一起使用的表达式
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
答案 1 :(得分:0)
我找到了一种方法来完成这项工作:(不确定是否有更好的方法可以做到这一点,如果有,请告诉我们)
首先使用与原始列相同的列(.Copy)创建一个新的DataSet,但删除行(.Clear)
DataSet dsInvoiceDataFiltered = new DataSet();
DataTable dtInvoiceDataFiltered = dsInvoiceData.Tables[0].Copy();
dtInvoiceDataFiltered.Clear();
其次创建一个DataTable来保存dtInvoiceDataFiltered中的LOGUID条目,其中VALUE!= 0
DataTable dtDistinctActivityPerLogUid = new DataTable("DistinctActivityPerLogUid");
dtDistinctActivityPerLogUid.Columns.Add(new DataColumn("Activity", typeof(string)));
dtDistinctActivityPerLogUid.Columns.Add(new DataColumn("Loguid", typeof(int)));
然后在原始数据集上应用过滤器逻辑
foreach (DataRow dr in dsInvoiceData.Tables[0].Rows)
{
int iVALUE = Convert.ToInt32(dr["VALUE"]);
int iLogUid = Convert.ToInt32(dr["LOGUID"]);
string sActivity = Convert.ToString(dr["ACTIVITY"]);
// This is a unique situation (MOVE)
// and must be added to the new dataset
if (VALUE == 0)
{
dtInvoiceDataFiltered.ImportRow(dr);
}
else
{
// This is a STORE/OTHER LOGUID with multiple VALUES
// only 1 of them should be added to the new list
// Check if there is already a VALUE != 0 for that LOGUID
DataRow[] drRowsThatMatchLogUID = dtDistinctActivityPerLogUid.Select("Loguid = '" + iLogUid + "' AND Activity = '" + sActivity + "' ");
if (drRowsThatMatchLogUID.Length == 0)
{
// If there isn't one in yet, add 1 to the new list
// and to the Datatable to keep track of LOGUID entries
dtInvoiceDataFiltered.ImportRow(dr);
DataRow drNewLogUID = dtDistinctActivityPerLogUid.NewRow();
drNewLogUID["Activity"] = sActivity;
drNewLogUID["Loguid"] = iLogUid;
dtDistinctActivityPerLogUid.Rows.Add(drNewLogUID);
}
}
}
// dsInvoiceDataFiltered is now the DataSet that contains the filtered solution
dsInvoiceDataFiltered.Tables.Add(dtInvoiceDataFiltered);
PS:因为我必须使用.Net 2.0
,所以不使用linq