DataTable-只选择查找值小于10的行

本文关键字:小于 的行 查找 选择 DataTable- | 更新日期: 2023-09-27 17:58:43

我很难弄清楚DataTable的Select方法发生了什么。这是我在一个名为VotePeriods:的DataTable中得到的数据

PeriodID Description

11 Test 11

10 Test 10

9 Test 9

1 Test1

以下是根据PeriodID:选择周期的代码

if (VotePeriods.Rows.Count > 0)
{
    DataRow[] vp = VotePeriods.Select("PeriodID = " + voteperiod);
    if (vp.Length > 0)
    {
        return vp[0];
    }
}

出于某种原因,如果voteperiod为9或更小,那么我选择了正确的行。但是,如果我传入1011,即使在我的DataTable中存在PeriodID 1011,我也没有返回的数据。有什么建议吗?

谢谢。

DataTable-只选择查找值小于10的行

希望下面能起作用。在对DataTable使用select方法时,请记住始终为值添加单引号。

if (VotePeriods.Rows.Count > 0)
{
    DataRow[] vp = VotePeriods.Select("PeriodID = '" + voteperiod +"'");
    if (vp.Length > 0)
    {
        return vp[0];
    }
}

您是否尝试过使用LINQ进行选择?我的DataTable运气不好。以前选择过。

if(VotePeriods.Rows.Count > 0)
{
      var vp = from x in VotePeriods.AsEnumerable()
               where x["PeriodID"] == voteperiod
               select x;
}

确保PeriodID列的数据类型为数字,如int:

VotePeriods.Columns.Add("PeriodID", typeof(int));