LINQ查询到细胞结合的结果? [英] Linq query to combine cell results?

查看:90
本文介绍了LINQ查询到细胞结合的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,这个样本数​​据。

I have a data table with this sample data.

需求量的就是让这个数据表,以便返回所有的行,但重复的行(过去两年为例)的要求是通过使一个记录与一个逗号和回报有不同的价值观结合起来。

Requirment is to make this data table so it returns all rows but for duplicate rows (last two for example) requirement is to combine there different values with a comma and return by making it one record.

例如:

金银,Metllurgist 19尼克·戈登。 。 。 。 。

Gold-Silver,Metllurgist 19 Nick Gordon . . . . .

请帮我。

感谢

推荐答案

由于专业领域是唯一变化的领域,你想为这个领域的综合价值,需要结果继
1. 分组根据行业结果
2. 选择记录结果
3.使用 总结 为结合行业

As Profession field is the only changing field, and you want combined value for this field, Following is needed
1. Grouping based on Profession
2. Select the records
3. Use Aggregate for combining Profession

您还需要定义一个具体类型重新present ProfessionRecord 在$ C $下面C)

You also need to define a concrete type to represent Row (ProfessionRecord in code below)

 var results = dataTable.AsEnumerable()
                   .GroupBy(x=> x.Field<string>("Profession"))
                   .Select (  grouping => 
                               new {
                                  Key=grouping.Key,
                                  CombinedProfession = grouping.Aggregate( (a,b)=> a + " " + b)
                                })
                    .Select (x=> new ProfessionRecord
                            {
                              Id = x.Key.Id,
                              Name = x.Key.Name,
                              Profesion = x.CombinedProfession,
                            });

VB.NET从工具

 Dim results = dataTable.AsEnumerable().GroupBy(Function(x) x.Field(Of String)("Profession")).[Select](Function(grouping) New With { _
    .Key = grouping.Key, _
    .CombinedProfession = grouping.Aggregate(Function(a, b) a + " " + b) _
}).[Select](Function(x) New ProfessionRecord() With { _
    .Id = x.Key.Id, _
    .Name = x.Key.Name, _
    .Profesion = x.CombinedProfession _
})

(我不知道它是多么优化为VB.NET是不是我的第一语言)

(I am not sure how much it is optimized as VB.NET is not my first language)

您需要添加数据行扩展的参考该方法字段&LT; T&GT;(串字段名)

这篇关于LINQ查询到细胞结合的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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