C# 为所有列值筛选Datatable的特定行
我的应用程序中有一个datatable,它只有一行,如下所示C# 为所有列值筛选Datatable的特定行,c#,asp.net,C#,Asp.net,我的应用程序中有一个datatable,它只有一行,如下所示 pcode d1 d2 d3 d4 d5 d6 10001 0 1 1 0 1 1 现在我想过滤datatable,只得到除pcode列(即我只需要d2、d3、d5、d6列)之外的值为1的列。上面的datatable来自数据库。有没有办法过滤datatable,或者如果我可以用database table做这件事,我该怎么做?有什么建议吗?听起来像: List<string> result = dt.Col
pcode d1 d2 d3 d4 d5 d6
10001 0 1 1 0 1 1
现在我想过滤datatable,只得到除pcode列(即我只需要d2、d3、d5、d6列)之外的值为1的列。上面的datatable来自数据库。有没有办法过滤datatable,或者如果我可以用database table做这件事,我该怎么做?有什么建议吗?听起来像:
List<string> result = dt.Columns.Cast<DataColumn>()
.Where(c => c.ColumnName != "pcode")
.Where(c => dt.Rows[0][c].ToString() == "1")
.Select(c => c.ColumnName)
.ToList();
List result=dt.Columns.Cast()
.Where(c=>c.ColumnName!=“pcode”)
.Where(c=>dt.Rows[0][c].ToString()=“1”)
.选择(c=>c.ColumnName)
.ToList();
听起来像:
List<string> result = dt.Columns.Cast<DataColumn>()
.Where(c => c.ColumnName != "pcode")
.Where(c => dt.Rows[0][c].ToString() == "1")
.Select(c => c.ColumnName)
.ToList();
List result=dt.Columns.Cast()
.Where(c=>c.ColumnName!=“pcode”)
.Where(c=>dt.Rows[0][c].ToString()=“1”)
.选择(c=>c.ColumnName)
.ToList();
如何将此变量转换为字符串?如果(dtble.Rows.Count>0){List result=dtble.Columns.Cast()。其中(c=>dtble.Rows[0]。字段(c)==1&&c.ColumnName!=“ProductCode”)。选择(c=>c.ColumnName)。ToList();}}我遇到以下错误“无法将类型为'System.Int32'的对象强制转换为类型为'System.String'。如何将此变量转换为字符串?如果(dtble.Rows.Count>0){List result=dtble.Columns.cast()。其中(c=>dtble.Rows[0]。字段(c)==1&&c.ColumnName!=“ProductCode”)。请选择(c=>c.ColumnName).ToList();}}我收到了以下错误:“无法将'System.Int32'类型的对象强制转换为'System.String'类型。”