【问题标题】:DataTable returning empty数据表返回空
【发布时间】:2021-11-29 14:55:08
【问题描述】:

对于我的应用程序,有几个单独的数据表,我需要根据匹配的 id 创建一个新的数据表。我必须做几次这个过程,所以我创建了一个函数,所以我没有重复代码,我已经这样做了:

    private static DataTable CloneTable(DataTable originalTable, DataTable newTable, DataTable targetTable,
        string addedColumn, string columnToExtract, bool multipleConditions = false, string secondColumnName = null, string secondColumnConditon= null)
    {

        newTable = originalTable.Clone();
        newTable.Columns.Add(addedColumn);

        foreach (DataRow row in originalTable.Rows)
        {

            DataRow[] rowsTarget;

            if (multipleConditions == false)
            {
                rowsTarget = targetTable.Select(string.Format("ItemId='{0}'", Convert.ToString(row["ItemId"])));
            } else
            {
               rowsTarget = targetTable.Select(string.Format("ItemId='{0}' AND {1} ='{2}'", Convert.ToString(row["ItemId"]), secondColumnName, secondColumnConditon));
            }


            if (rowsTarget != null && rowsTarget.Length > 0)
            {
                string data = rowsTarget[0][columnToExtract].ToString();
                var lst = row.ItemArray.ToList();
                lst.Add(data);
                newTable.Rows.Add(lst.ToArray());
            }

            else
            {
                string data = "";
                var lst = row.ItemArray.ToList();
                lst.Add(data);
                newTable.Rows.Add(lst.ToArray());
            }

        }

            return newTable;

    }

然后我在一个单独的函数中调用它,如下所示:

    private DataTable GetExtractData()
    {

.........................

            DataTable includeLastModified = new DataTable();
            DataTable includeFunction = new DataTable();
            DataTable includeDiscipline = new DataTable();


            CloneTable(itemsTable, includeLastModified, lastModifiedTable, "LastModifiedDate", "LastModifiedDate");
            CloneTable(includeLastModified, includeFunction, customPropertiesTable, "Function", "ItemTitle", true, "Title", "Function");
            CloneTable(includeFunction, includeDiscipline, customPropertiesTable, "Discipline", "ItemTable", true, "Title", "Discipline");

return includeDiscipline;


}

我遇到的问题是这里的数据表返回空,我不知道为什么。 在我的 CloneTable 函数中,我执行了以下操作以确保新表不为空:

        foreach (DataRow row in newTable.Rows)
        {
            foreach (var item in row.ItemArray)
            {
                Console.WriteLine(item);
            }
        }

它不是空的,所以我不确定为什么当我在单独的函数中返回它时它现在是空的?

我对 GetData 函数中的 includeDiscipline 表调用了同样的方法,但它返回为空。

没有错误,但有一条消息来来去去,说参数“newTable”可以删除,因为不使用初始值。我不确定这是怎么回事,因为它显然被使用了?

我假设这可能是我调用函数的方式,但我真的不确定我在这里做错了什么

【问题讨论】:

    标签: c# datatable


    【解决方案1】:

    好吧,脸掌时刻,刚刚意识到我忘了把它分配给某个东西。

    所以如果我这样做:

    var test =  CloneTable(itemsTable, includeLastModified, lastModifiedTable, "LastModifiedDate", "LastModifiedDate");
    
    
    return test;
    

    它工作正常,不再返回空

    【讨论】:

      猜你喜欢
      • 2018-12-16
      • 2015-01-03
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 2019-02-13
      • 1970-01-01
      相关资源
      最近更新 更多