我有一个datatable
dtTable,其中有几列,其中一列称为Value。
">值"列的类型为int64
,具有以下值:
12356 0 78 4 0
现在我想获取那些在">值"列中的值大于零的行。即,我想要上面示例中的以下行:
12356 78 4
我认为 linq 适用于这种情况,任何解决方案都会有所帮助。
像这样:
DataTable tblFiltered = dtTable.AsEnumerable()
.Where(r => r.Field<Int>("Value") > 0)
.CopyToDataTable();
其中 Value 是数据类型为 int 的列名。
顺便说一下,一个简单的谷歌搜索就可以解决你的查询。
OP:它是一个 64 位整数,可以为空:
DataTable tblFiltered = dtTable.AsEnumerable()
.Where(r => r.Field<Int64?>("Value") > 0)
.CopyToDataTable();
var res = db.dtTable.Where(x=> x.Value > 0).ToList();
OP:不,它不是数据库表,而是数据表。
DataTable filteredTable = dtTable.AsEnumerable().Where(row => row.Field<int>("Value") > 0).CopyToDataTable();
OP:抱歉,数据类型是 int64,它给出以下错误"无法将 DBNull.Value 转换为类型"System.Int64"。请使用可为空的类型。
DataTable filteredTable = dtTable.AsEnumerable().Where(row => row.Field<Int64?>("Value") > 0).CopyToDataTable();
你是什么意思? 用样本数据显示您的结果。目前,我是这样理解的。请尝试以下操作:
select t.* from table t where t.value > 0