Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 影响C中的datatable列值_C#_Datatable - Fatal编程技术网

C# 影响C中的datatable列值

C# 影响C中的datatable列值,c#,datatable,C#,Datatable,我有一个如下的数据表 Name Val ID -------------- Raja 0 101 --------------- Ram 0 102 --------------- Ray 0 103 --------------- Raja 0 104 --------------- 如果相同的名称再次重复,我想影响我的datatable列。在我的datatable中,Raja名称再次重复,因此我想将名称更改为Raja2 O/p: 请引导我

我有一个如下的数据表

Name    Val  ID
--------------
Raja    0   101
---------------
Ram     0   102
---------------
Ray     0   103
---------------
Raja    0   104
---------------
如果相同的名称再次重复,我想影响我的datatable列。在我的datatable中,Raja名称再次重复,因此我想将名称更改为Raja2

O/p:

请引导我去拿这个

DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Item", typeof(string));
            table.Columns.Add("Qty", typeof(string));

            table.Rows.Add("AAA", "Indocin", "David");
            table.Rows.Add("XXX", "Enebrel", "Sam");
            table.Rows.Add("AAA", "Hydralazine", "Christoff");
            table.Rows.Add("CCC", "Combivent", "Janet");
            table.Rows.Add("AAA", "Dilantin", "Melanie");
           // Query with Linq
           var newNames = table.AsEnumerable()
                                .Select((row, rowIndex) => new { rowIndex, index }) // storing row and index of it in anonymous type
                                .GroupBy(x => x.row.Field<string>("Name"))          // group by Name
                                .Where(g => g.Count() > 1)                          // take only duplicates
                                .SelectMany(g => g                                  // select all rows but first of duplicates
                                    .Where(x => x.rowIndex > 0)
                     /*Error*/               .Select((x, dupIndex) => new { x.rowIndex, newName = $"{g.Key}({dupIndex + 1})" }));    

            foreach(var x in newNames)
                table.Rows[x.index].SetField("Name", x.newName);
            /***********/
这里是我的代码。

您可以使用Linq来创建数据表,尤其是GroupBy:


我按名称分组,然后只取重复的行+它们的索引。从那些重复的组中,我只取第一个,因为那个名字仍然存在

不是免费的代码编写服务。您应该尝试自己编写代码。之后,如果你有问题,你可以张贴你已经尝试了一个明确的解释什么是不工作,并提供一个解决方案。我建议你读一个好的问题和答案。另外,请确保在此行中出现.Cod填充错误:。Selectx=>new{x.index,newName=${g.Key}{x.index+2}@莫尼:我编辑了我的答案,并添加了一些解释。如果您还有其他问题,请询问。它在@Tim.Selectx,dupIndex=>new{x.index,D=${g.Key}{dupIndex+1}进行测试;这行显示了一个错误,我不能使用它,请帮助我fix@Moni当前位置既然你不提这个错误,我就帮不了你。正如我告诉你的,这是经过测试的,并且有效。因此,对于您来说,必须有一些不同的东西,例如框架或您没有使用System.Linq;添加的东西;。但我不知道您是否没有提到errorError::Invalid anonymous type成员声明符。匿名类型成员必须使用成员分配、简单名称或成员访问权限声明。
DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Item", typeof(string));
            table.Columns.Add("Qty", typeof(string));

            table.Rows.Add("AAA", "Indocin", "David");
            table.Rows.Add("XXX", "Enebrel", "Sam");
            table.Rows.Add("AAA", "Hydralazine", "Christoff");
            table.Rows.Add("CCC", "Combivent", "Janet");
            table.Rows.Add("AAA", "Dilantin", "Melanie");
           // Query with Linq
           var newNames = table.AsEnumerable()
                                .Select((row, rowIndex) => new { rowIndex, index }) // storing row and index of it in anonymous type
                                .GroupBy(x => x.row.Field<string>("Name"))          // group by Name
                                .Where(g => g.Count() > 1)                          // take only duplicates
                                .SelectMany(g => g                                  // select all rows but first of duplicates
                                    .Where(x => x.rowIndex > 0)
                     /*Error*/               .Select((x, dupIndex) => new { x.rowIndex, newName = $"{g.Key}({dupIndex + 1})" }));    

            foreach(var x in newNames)
                table.Rows[x.index].SetField("Name", x.newName);
            /***********/
var newNames = table.AsEnumerable()
    .Select((row, rowIndex) => new { row, rowIndex }) // storing row and index of it in anonymous type
    .GroupBy(x => x.row.Field<string>("Name"))          // group by Name
    .Where(g => g.Count() > 1)                          // take only duplicates
    .SelectMany(g => g                                  // select all rows but first of duplicates
        .Where(x => x.rowIndex > 0)
        .Select((x, dupIndex) => new { x.rowIndex, newName = $"{g.Key}({dupIndex + 2})" }));    

foreach(var x in newNames)
    table.Rows[x.rowIndex].SetField("Name", x.newName);