查找重复和合并记录到单个datatable c# [英] Find duplicate and merge record into single datatable c#

查看:546
本文介绍了查找重复和合并记录到单个datatable c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从DataTable行中找到重复的内容。如下:

  var groups = table.AsEnumerable()
.GroupBy(r => new
{
c1 = r.Field< String>(Version),
});
var tblDuplicates = groups
.Where(grp => grp.Count()> 1)
.SelectMany(grp => grp)
.CopyToDataTable();

现在,我想将所有重复的记录合并为单个,并将其总和为Value列值。 / p>

非常像以下内容:



DataTable with Duplicates:

 版本价值
1 2

2 2

2 1

1 3

2 1

3 2

DataTable没有重复,价值相加。:

 版本价值
1 5

2 4

3 2

我知道这个链接反思的帮助。
http://forums.asp.net/t/1570562.aspx/1



还有其他方式吗?



编辑:
但是,如果我有两列,如五列,我仍然希望在Value列上进行总和,并且还需要在resulatant summed datatable中的其他列数据。怎么做?在这里我得到了我的结果DataTable的版本和价值。我也想要其他列的值。如下:



版本col1 col2值

  1 AA 2 

2 BB 2

2 BB 1

1 AA 3

2 BB 1

3 CC 2


解决方案

  var result = table.AsEnumerable()
.GroupBy(r => r.Field< string>(Version))
.Select(g =>
{
var row = table.NewRow();
row.ItemArray = new object []
{
g.Key,
g.Sum(r => r .Field< int>(Value))
};
return row;
})。CopyToDataTable();

编辑:



如果要保留其他字段,请尝试以下:

  var result = table.AsEnumerable()
.GroupBy(r => new
{
Version = r.Field< String>(Version),
Col1 = r.Field< String>(Col1),
Col2 = r.Field< String>(Col2)
})
.Select(g =>
{
var row = g.First ;
row.SetField(Value,g.Sum(r => r.Field< int>(Value)));
return row;
})CopyToDataTable ();


I am able to find the duplicates out of DataTable rows. Like following:

var groups = table.AsEnumerable()
        .GroupBy(r => new
        {
            c1 = r.Field<String>("Version"),
        });
        var tblDuplicates = groups
            .Where(grp => grp.Count() > 1)
            .SelectMany(grp => grp)
            .CopyToDataTable();

Now, I want to merge all the duplicate records in to single and sum it's Value column value.

Pretty much like following:

DataTable with Duplicates:

Version   Value  
1  2

2  2

2  1

1  3

2  1

3  2 

DataTable with no duplicates and Value summed.:

Version   Value  
1  5

2  4

3  2 

I am aware about this link which does this with the help of reflection. http://forums.asp.net/t/1570562.aspx/1

Anyother way to do it?

Edit: However, if I have more than two columns, like five columns and I still want to do the sum on Value column and also need other columns data in resulatant summed datatable. How to do it? Here I get the Version and Value in my result DataTable. I want other columns with values also. Like following:

Version col1 col2 Value

1  A A 2

2  B B 2

2  B B 1

1  A A 3

2  B B 1

3  C C 2 

解决方案

var result = table.AsEnumerable()
            .GroupBy(r => r.Field<string>("Version"))
            .Select(g =>
                {
                    var row = table.NewRow();
                    row.ItemArray = new object[]
                        {
                            g.Key, 
                            g.Sum(r => r.Field<int>("Value"))
                        };
                    return row;
                }).CopyToDataTable();

Edit:

If you want to keep other field, try below:

var result = table.AsEnumerable()
            .GroupBy(r => new
                {
                     Version = r.Field<String>("Version"),
                     Col1 =  r.Field<String>("Col1"),
                     Col2 =  r.Field<String>("Col2")
                })
            .Select(g =>
                {
                    var row = g.First();
                    row.SetField("Value", g.Sum(r => r.Field<int>("Value")));
                    return row;
                }).CopyToDataTable();

这篇关于查找重复和合并记录到单个datatable c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆