通过VB.NET中的LINQ将通用列表分组为2个值并从单个数据集连接

时间:2011-06-27 03:24:50

标签: linq lambda vb.net-2010

我班级的定义

    Public Class ProcessAlert
       Public LoanNumber As String
       Public EmailAddress As String
       Public AlertType As String
       Public AlertMethodID As Byte
    End Class

代表从DB

返回的数据的通用列表
     Dim a As New List(Of ProcessAlert)

     a.Add(New ProcessAlert("0000112367", "5551110000@txt.att.net", "Alert", 2))
     a.Add(New ProcessAlert("0000112367", "5551110000@txt.att.net", "Document", 2))
     a.Add(New ProcessAlert("0000112367", "5551110000@txt.att.net", "Note", 2))
     a.Add(New ProcessAlert("0000112367", "jdoe@home.com", "Alert", 1))
     a.Add(New ProcessAlert("0000112367", "jdoe@home.com", "Document", 1))
     a.Add(New ProcessAlert("0000112367", "jdoe@home.com", "Note", 1))

     Return a

我管理了这个LINQ语句,它将由LoanNumber和EmailAddress分组,但我无法将AlertType显示为所有3个值的连接字段

     Dim res = Alerts.GroupBy(Function(g) New With {Key .A = g.LoanNumber, Key .B = g.EmailAddress}) 

此代码将通过EmailAddress和我需要的值进行分组:         “jdoe@home.com”,“警报,文件,注意”         “5551110000@txt.att.net”,“警报,文件,注意”

    Dim res = Alerts.GroupBy(Function(i) i.EmailAddress).Select(Function(g) New KeyValuePair(Of String, String)(g.Key, String.Join(",", g.Select(Function(x) x.AlertType).ToArray())))

但我似乎无法得到声明给我一个看起来像这样的回报:

    "0000112367","jdoe@home.com", "Alert, Document, Note"
    "0000112367","5551112222@txt.att.net", "Alert, Document, Note"

如何将2个字段分组,然后将AlertType的值连接到单个字段中?

提前致谢

1 个答案:

答案 0 :(得分:0)

你在寻找这样的东西:

From _a In a
Group _a.AlertType By _a.LoanNumber, _a.EmailAddress Into Group
Select New With
{
    .LoanNumber = LoanNumber,
    .EmailAddress = EmailAddress,
    .AlertTypes = String.Join(", ", Group.ToArray())
}