Grouping a generic list by 2 values and concatenation from a single dataset via LINQ in VB.NET
Definition of my Class
Public Class ProcessAlert
Public LoanNumber As String
Public EmailAddress As String
Public AlertType As String
Public AlertMethodID As Byte
End Class
A generic list that is representative of data returned from DB
Dim a As New List(Of ProcessAlert)
a.Add(New ProcessAlert("0000112367", "5551110000@txt.att.net", "Alert", 2))
a.Add(New ProcessAlert(开发者_C百科"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
I managed this LINQ statement that will group by LoanNumber and EmailAddress but I can't get the AlertType to show up as a concatenated field of all 3 values
Dim res = Alerts.GroupBy(Function(g) New With {Key .A = g.LoanNumber, Key .B = g.EmailAddress})
This code will give me a grouping by EmailAddress and the values I need of: "jdoe@home.com", "Alert, Document, Note" "5551110000@txt.att.net", "Alert, Document, Note"
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())))
But I cannot seem to get the statement give me a return that looks like this:
"0000112367","jdoe@home.com", "Alert, Document, Note"
"0000112367","5551112222@txt.att.net", "Alert, Document, Note"
How do I get 2 fields to group and then concatenate the values of AlertType into a single field???
Thanks in advance
Are you looking for something like this:
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())
}
精彩评论