从数据集中添加到 int 数组

发布于 2024-10-03 04:15:45 字数 372 浏览 0 评论 0原文

我必须将状态值与“Active”相同的数据集中的所有 Id 添加到 int 数组 PromotionID 中。这怎么可能。我缺少什么?

int[] promotionID;
foreach (DataRow dr in ds.Tables[0].Rows[i]["Status"].ToString() == "Active")
{

    promotionID = new int[] { Convert.ToInt32(dr["Id"]) };
}

错误是:

foreach 语句无法对“bool”类型的变量进行操作,因为“bool”不包含“GetEnumerator”的公共定义

I have to add all the Id from the dataset that has Status value same as "Active" to the int array PromotionID. How is that possible. What am i missing?

int[] promotionID;
foreach (DataRow dr in ds.Tables[0].Rows[i]["Status"].ToString() == "Active")
{

    promotionID = new int[] { Convert.ToInt32(dr["Id"]) };
}

The error is:

foreach statement cannot operate on variables of type 'bool' because 'bool' does not contain a public definition for 'GetEnumerator'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听风吹 2024-10-10 04:15:45

我建议使用 LINQ:

int[] promotionIDs = (from dr in ds.Tables[0].AsQueryable()
                      where dr.Field<string>("Status") == "Active"
                      select dr.Field<int>("Id")).ToArray();

如果您想修复代码,让我告诉您它有什么问题:

foreach (DataRow dr in ds.Tables[0].Rows[i]["Status"].ToString() == "Active")

i 来自哪里?您正在使用 foreach,因此不需要计数器变量。您的循环应如下所示:

foreach (DataRow dr in ds.Tables[0].Rows) {
    if (dr.Field<string>("Status") == "Active") {
        ...
    }
}

现在,如何将 Id 添加到数组中。您在这里所做的...

promotionID = new int[] { Convert.ToInt32(dr["Id"]) };

...是创建一个带有一个值的数组(丢弃其中的所有内容),该值是当前记录的 Id。数组不是添加项目的良好数据结构。我建议使用 List:

List<int> promotionIDs = new List<int>();

foreach (DataRow dr in ds.Tables[0].Rows) {
    if (dr.Field<string>("Status") == "Active") {
        promotionIDs.Add(dr.Field<int>("Id"));
    }
}

如果您仍然需要数组,可以稍后将其转换:

int[] promotionIDArray = promotionIDs.ToArray();

I suggest to use LINQ:

int[] promotionIDs = (from dr in ds.Tables[0].AsQueryable()
                      where dr.Field<string>("Status") == "Active"
                      select dr.Field<int>("Id")).ToArray();

If you want to fix your code instead, let me tell you what's wrong with it:

foreach (DataRow dr in ds.Tables[0].Rows[i]["Status"].ToString() == "Active")

Where does the i come from? You're using foreach, so you don't need a counter variable. Your loop should look like this:

foreach (DataRow dr in ds.Tables[0].Rows) {
    if (dr.Field<string>("Status") == "Active") {
        ...
    }
}

Now, how to add the Id to the array. What you are doing here...

promotionID = new int[] { Convert.ToInt32(dr["Id"]) };

...is to create a new array (throwing away everything that was in it) with one value, which is the Id of the current record. Arrays are not good data structures for adding items. Let me suggest to use a List instead:

List<int> promotionIDs = new List<int>();

foreach (DataRow dr in ds.Tables[0].Rows) {
    if (dr.Field<string>("Status") == "Active") {
        promotionIDs.Add(dr.Field<int>("Id"));
    }
}

If you still need an array, you can convert it afterwards:

int[] promotionIDArray = promotionIDs.ToArray();
动次打次papapa 2024-10-10 04:15:45

你会想要这样的东西:

List<int> promotionLst = new List<int>();
foreach (DataRow dr in ds.Tables[0].Rows) {
    if (dr["Status"].ToString() == "Active") {
        promotionLst.Add(Convert.ToInt32(dr["Id"]));
    }
}
int[] promotion = promotionLst.ToArray();

You'll want something like this:

List<int> promotionLst = new List<int>();
foreach (DataRow dr in ds.Tables[0].Rows) {
    if (dr["Status"].ToString() == "Active") {
        promotionLst.Add(Convert.ToInt32(dr["Id"]));
    }
}
int[] promotion = promotionLst.ToArray();
旧时模样 2024-10-10 04:15:45

不能在 foreach 循环中使用过滤条件。试试这个:

int[] promotion;
foreach (DataRow dr in ds.Tables[0].Rows)
{
    if (dr["Status"].ToString() == "Active")
       promotionID = new int[] { Convert.ToInt32(dr["Id"]) };
}

这可以解决您问题的错误部分,但是您对 promotionID 的使用看起来不正确,因为您在每个正面匹配上都覆盖了它。您应该使用 List 而不是 int[] 并使用 promotion.Add(Convert.ToInt32(dr["Id"])) 将号码添加到列表中。看起来像:

var promotion = new List<int>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
    if (dr["Status"].ToString() == "Active")
       promotion.Add(Convert.ToInt32(dr["Id"]));
}

另一个选择是 LINQ,正如 Heinzi 演示的

You can't use a filter condition in the foreach loop. Try this:

int[] promotion;
foreach (DataRow dr in ds.Tables[0].Rows)
{
    if (dr["Status"].ToString() == "Active")
       promotionID = new int[] { Convert.ToInt32(dr["Id"]) };
}

That takes care of the error portion of your question, however your use of promotionID looks incorrect since you're overwriting it on each positive match. You should use a List<int> instead of int[] and use promotion.Add(Convert.ToInt32(dr["Id"])) to add numbers to the list. That looks like:

var promotion = new List<int>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
    if (dr["Status"].ToString() == "Active")
       promotion.Add(Convert.ToInt32(dr["Id"]));
}

Another option is LINQ as Heinzi demonstrated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文