首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在datatable中追加记录

在datatable中追加记录
EN

Stack Overflow用户
提问于 2013-03-08 01:30:14
回答 1查看 159关注 0票数 2

我已经在其他地方发布了这篇文章,但我对这一要求的理解是不正确的,因为帮助我的版主@ShaiCohen建议我转发它。

我需要编辑提供给DataTable的数据,这些数据可以有不同的列和行,但是前两列是不变的。数据如下所示(行开头的数字不是数据的一部分):

代码语言:javascript
运行
复制
    Repair  Repair 
    Code    Code Entries          6/1/2012  7/1/2012  8/1/2012  9/1/2012
    ------  --------------------  --------  --------  --------  --------
1.  00000A  Critical Down Time       1       
2.  00000A  Critical Outage          1       
3.  00000A  Total Repair Time        65         
4.  00000B  Critical Down Time                                     6
5.  00000B  Total Repair Time                                      90
6.  00000C  Critical Down Time       1          5    
7.  00000C  Critical Outage          1          5    
8.  00000C  Total Repair Time        30         240    
9.  00000D  Critical Down Time                                     2     
10. 00000E  Critical Down Time                          1    
11. 00000G  Critical Down Time                                     1    
12. 00000M  Critical Down Time        1                            3    
13. 00000M  Critical Outage           1                 3    
14. 00000M  Total Repair Time         60                180   

请注意,行1-3、6-8具有相同的维修代码类别,因此被视为组。另一方面,10-12行只有"Critical Down Time“子类别,而其余的则是三者的组合。

要求是在"Repair Code Entries“子类别中插入它们不存在的地方。它们不存在的原因是因为数据库中没有数据,但是客户端希望即使没有相应的数据也显示缺少的词语,并插入一个空行来分隔组,如下所示:

代码语言:javascript
运行
复制
    Repair  Repair 
    Code    Code Entries          6/1/2012  7/1/2012  8/1/2012  9/1/2012
    ------  --------------------  --------  --------  --------  --------
1.  00000A  Critical Down Time       1       
2.  00000A  Critical Outage          1       
3.  00000A  Total Repair Time        65         

4.  00000B  Critical Down Time                                     6
    00000B  Critical Outage          
5.  00000B  Total Repair Time                                      90

6.  00000C  Critical Down Time       1          5    
7.  00000C  Critical Outage          1          5    
8.  00000C  Total Repair Time        30         240 

9.  00000D  Critical Down Time                                     2   
    00000D  Critical Outage          
    00000D  Total Repair Time        

然而,当前的代码假设数据总是有三个子类别的分组,因此当它为no时,前一行中的子类别将覆盖当前行中的子类别:

代码语言:javascript
运行
复制
8.  00000C  Total Repair Time        30         240 

9.  00000D  Total Repair Time (should be Critical Down Time)        2   
    00000D  Critical Outage          

在下面的代码中,当处理新行时,CheckSubCategoryRequirements方法中的计数器subCategoryOccurences不会被重置为零。

代码语言:javascript
运行
复制
public void PrepareDataTable(DataTable dtResults)
{

    if (dtResults == null || dtResults.Rows.Count == 0)
       return;

    //initialize category
    categoryPrevious = dtResults.Rows[0]["Category"].ToString();
    do
    {
        //get the current category
        categoryCurrent = dtResults.Rows[rowCount]["Category"].ToString();
        //check if this is a new category. this is where all the work is done
        if (categoryCurrent != categoryPrevious)
        {
            //check if we have fulfilled the requirement for number of subcategories 
            CheckSubCategoryRequirements(dtResults);
            //at this point we have fulfilled the requirement for number of subcategories 
            //add blank (separator) row
            dtResults.Rows.InsertAt(dtResults.NewRow(), rowCount);
            rowCount++;
            //reset the number of subcategories
            subCategoryOccurences = 0;
            categoryPrevious = categoryCurrent;
        }
        else
        {
            rowCount++;
            categoryOccurences++;
        }
    } while (rowCount < dtResults.Rows.Count);
    //check sub category requirements for the last category
    CheckSubCategoryRequirements(dtResults);  
}




private void CheckSubCategoryRequirements(DataTable dtResults)
{
    if (subCategoryOccurences< subCategories.Length)
    {
        //we need to add rows for the missing subcategories
        while (subCategoryOccurences< subCategories.Length)
        {
            //create a new row and populate category and subcategory info
            rowFiller = dtResults.NewRow();
            rowFiller["Category"] = categoryPrevious;
            rowFiller["SubCategory"] = subCategories[subCategoryOccurences];
            //insert the new row into the current location of table 
            dtResults.Rows.InsertAt(rowFiller, rowCount);
            subCategoryOccurences++;
            rowCount++;
        }
    }
}

我试图在方法调用之前移动计数器,但这导致了不希望的结果,所以我不确定从哪里开始。我希望得到有建设性的意见。谢谢。R.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-08 03:30:36

对于这些需求,我采用了与以前不同的方法。有了这些新的需求,我们将不得不“倒退”,在以前缺少的地方插入子类别。

此方法创建一个新表,该表为原始表中存在的每个类别填充正确数量的子类别。一旦创建了新行,我们就会检查旧表,看看是否有需要复制到新表中的数据(例如:示例中的"6/1/2012“和"7/1/2012”)。

尝试以下代码:

代码语言:javascript
运行
复制
public DataTable PrepareDataTable(DataTable dtResults)
{
    string[] subCategories = new string[3] {"Critical Down Time", "Critical Outage", "Total Repair Time"};
    //make a copy of the original table
    DataTable dtOutput = dtResults.Clone();
    DataRow drOutput = null;
    DataRow[] drResults = null;
    //retrieve the list of Categories
    var categories = dtResults.AsEnumerable().Select(r => r["Category"]).Distinct().ToList();
    //populate the new table with the appropriate rows (combinations of categories/subcategories)
    foreach (string category in categories)
    {
        for (int i = 0; i < subCategories.Length    ; i++)
        {
            //create the new row in the new table
            drOutput = dtOutput.NewRow();
            drOutput["Category"] = category;
            drOutput["SubCategory"] = subCategories[i];
            //here is where you will check to see if a row with the same category and subcategory exists in dtResults. if it does, then copy over the values for each column
            drResults = dtResults.Select(String.Format("Category = '{0}' AND SubCategory = '{1}'", category, subCategories[i]));
            if(drResults.Length > 0)
            {
                foreach(DataColumn column in dtResults.Columns)
                {
                    drOutput[column.ColumnName] = drResults[0][column.ColumnName];
                }

            }
            dtOutput.Rows.Add(drOutput);
        }
        //add filler/spacer row
        drOutput = dtOutput.NewRow();
        dtOutput.Rows.Add(drOutput);
    }
    return dtOutput;
}

下面是“测试工具”:

代码语言:javascript
运行
复制
public void RunTest()
{
    DataTable dtResults = new DataTable();
    dtResults.Columns.Add("Category");
    dtResults.Columns.Add("SubCategory");
    dtResults.Columns.Add("Data");
    dtResults.Rows.Add("00000A", "Critical Down Time", "1");
    dtResults.Rows.Add("00000A", "Critical Outage", "1");
    dtResults.Rows.Add("00000A", "Total Repair Time", "1");
    dtResults.Rows.Add("00000B", "Critical Down Time", "1");
    dtResults.Rows.Add("00000B", "Total Repair Time", "1");
    dtResults.Rows.Add("00000C", "Critical Down Time", "1");
    dtResults.Rows.Add("00000C", "Critical Outage", "1");
    dtResults.Rows.Add("00000C", "Total Repair Time", "1");
    dtResults.Rows.Add("00000D", "Critical Down Time", "1");
    dtResults.Rows.Add("00000E", "Critical Down Time", "1");
    dtResults.Rows.Add("00000G", "Critical Down Time", "1");
    dtResults.Rows.Add("00000M", "Critical Down Time", "1");
    dtResults.Rows.Add("00000M", "Critical Outage", "1");
    dtResults.Rows.Add("00000M", "Total Repair Time", "1");
    DataTable dtOutput = PrepareDataTable(dtResults);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15277849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档