寻址不可枚举数据表中的单个单元格

本文关键字:单个 单元格 不可枚举 数据表 寻址 | 更新日期: 2023-09-27 18:28:19

由于数据脏,我有一个数据表可能包含null或空单元格。在这种情况下,不能选择清除数据。

因为我需要每行输出一个奇异字符串(行的数量可能会改变,列的数量是常数(3)),所以我需要对每个单独的单元格进行寻址,以便告诉方法如果单元格恰好为空该怎么办。我最初尝试了以下内容:

数据集:con.dSet列表<>:官员

bool notNull(var cell)
{
    if (cell != null)
    {
        return true;
    }
    else
    {
        return false;
    }
}
void hereAreAllThePeople()
{
    if (con.dSet != null)
    {
        foreach (DataRow row in dSet.Tables[0].Rows)
        {
            string str = string.Empty;
            foreach (DataColumn col in row)
            {
                if (notNull)
                {
                    str += col.ToString();
                }
                else if (!notNull)
                {
                    str += "";
                }
            }
            Officers.Add(str);
        }
    }
}

它很难看,而且由于row不可枚举而失败。我该怎么过?

寻址不可枚举数据表中的单个单元格

是的,这是一个非常复杂的代码,但我想你可以这样做:

foreach (DataColumn col in dSet.Tables[0].Columns)
            {
                if (notNull)
                {
                    str += row[col].ToString();
                }
                else if (!notNull)
                {
                    str += "";
                }
            }

的,知道你有三个字段,

for (int i = 0; i <3; i++)
{
if (notNull)
                    {
                        str += row[i].ToString();
                    }
                    else if (!notNull)
                    {
                        str += "";
                    }
}

已编辑(因为某些原因我无法发表评论)好吧,我不确定你想要从中得到什么,但我没有看到notNull可变值在任何地方被设置,所以不确定你是如何使用它的。根据你问题的既定目标,这将如你所期望的那样工作:

void hereAreAllThePeople()
{
    if (con.dSet != null)
    {
        foreach (DataRow row in dSet.Tables[0].Rows)
    {
            Officers.Add(string.Join("", row.ItemArray.Select(p => (p == null) ? "": p.ToString()).ToArray()));
        }
        }
    }