C# 从c中的数据集中删除数据表#
在c#中,如果datatable的所有值都为零,我想从dataset中删除datatable 如何实现上述功能 我使用此代码向datatable中添加值C# 从c中的数据集中删除数据表#,c#,datatable,dataset,C#,Datatable,Dataset,在c#中,如果datatable的所有值都为零,我想从dataset中删除datatable 如何实现上述功能 我使用此代码向datatable中添加值 for (int row = startRowParcel + 1; row <= endRowParcel; row++) { List<string> rateRow = new List<string>(); for (int col = startColumnNsa; col <=
for (int row = startRowParcel + 1; row <= endRowParcel; row++) {
List<string> rateRow = new List<string>();
for (int col = startColumnNsa; col <= endColumnNsa; col++) {
if (Convert.ToString(ws.Cells[row, col].Value) == null)
rateRow.Add("0");
else if (Convert.ToString(ws.Cells[row, col].Value) == "1/2")
rateRow.Add("0.5");
else
rateRow.Add(Convert.ToString(ws.Cells[row, col].Value));
}
tbPriority.Rows.Add(rateRow.ToArray());
}
for(int row=startRowParcel+1;rowif(!(dt.AsEnumerable().Any)(x=>x.Field(“nameOfColumn”)!=0)){
//删除数据表
}
有关更多信息,请参见您可以使用以下代码实现此目的:
for(int i=0;i<dt.rows.count;i++)
{
for(intj=0;j<=dt.columns.count;j++)
{
if( dt.rows[i][j]!=0)
{
flag=1;
break;
}
}
}
if(flag==1)
{
// dont remove the table
}
else
{
ds.tables.remove(dt);
}
}
for(int i=0;ifor(int row=startRowParcel+1;row此LINQ方法查找所有行字段均为“0”的所有表:
var allZeroTables=dsPriorities.Tables.Cast()
.Where(tbl=>tbl.AsEnumerable()
.All(r=>tbl.Columns.Cast()
。全部(c=>r.Field(c)==“0”);
foreach(allZeroTables.ToList()中的DataTable zeroTable)
dsPriorities.Tables.Remove(零表);
是一种短路方法,在第一次不匹配时停止
请注意,ToList()
是必需的,因为在不创建新集合的情况下,您无法从foreach
中修改数据集的DataTableCollection
。如果全部为零,则不删除,为什么不检查列表中除0以外的值,如果为真,则将其添加到Datatable中?值是字符串,但不是可转换为int
,那么在这种情况下zero
是什么意思呢?@Sindhujampani查看TYY的答案,类似于我的答案,如果((rateRow.AsEnumerable().Any(x=>x!=“0”){tbPmServices.Rows.Add(rateRow.ToArray()),则会对您有所帮助}实际上我尝试过这个。但是它添加了行,即使整行都有“0”而不是!…Any(..!=0)
,您也可以简单地使用All(..==0)
我以前使用过它,但发现它比在第一个无效结果时退出的for…循环慢得多。但是在小数据表上,这种性能影响应该不会太明显。All()
将始终迭代所有集合。Any()
只迭代到第一次出现。是的,它的语法比较模糊,但是在一个大的集合中,任何
都会做得更好。实际上,如果整行包含“0”,而其他行不包含“0”,那么我希望这些行添加到数据表中。@TYYif(rateRow.Any(x=>x!=“0”))这是在添加行,即使整行都有零。@tyy无法对“System.Data.DataTable”类型的表达式应用索引。此错误出现在if(dt[i][j]!=0)@sindhu中的i位置。抱歉,编辑了代码。请现在重试。?if(dt.rows[i].columns[j]!=0)在列中显示错误,因为它没有扩展列sindu..您现在可以检查..我已经更新了if条件中的语法..if(Convert.ToString(dt.Rows[i][j])==“0”)它是这样工作的。无论如何,非常感谢您…我的要求得到了满足
for(int i=0;i<dt.rows.count;i++)
{
for(intj=0;j<=dt.columns.count;j++)
{
if( dt.rows[i][j]!=0)
{
flag=1;
break;
}
}
}
if(flag==1)
{
// dont remove the table
}
else
{
ds.tables.remove(dt);
}
}
for (int row = startRowParcel + 1; row <= endRowParcel; row++) {
List<string> rateRow = new List<string>();
for (int col = startColumnNsa; col <= endColumnNsa; col++) {
if (Convert.ToString(ws.Cells[row, col].Value) == null)
rateRow.Add("0");
else if (Convert.ToString(ws.Cells[row, col].Value) == "1/2")
rateRow.Add("0.5");
else
rateRow.Add(Convert.ToString(ws.Cells[row, col].Value));
}
if (rateRow.Any(x=> x != "0"))
tbPriority.Rows.Add(rateRow.ToArray());
}
//Then you can check if tbPriority has any rows if it doesn't then remove it from the dataset
if (!tbPriority.Rows.Any())
// delete dataTable
var allZeroTables = dsPriorities.Tables.Cast<DataTable>()
.Where(tbl => tbl.AsEnumerable()
.All(r => tbl.Columns.Cast<DataColumn>()
.All(c => r.Field<string>(c) == "0")));
foreach (DataTable zeroTable in allZeroTables.ToList())
dsPriorities.Tables.Remove(zeroTable);