Hi All,

I have a collection of huge records (6000+) wherein the object in the collections has got 50+ properties, so ideally it is not a good idea to run a "For-Each" loop here.
What I want is a "GroupBy" clause which would get me a Dictionary (Of String, String) from this collection where the key is the primary key (A) of this object and the value would be concatenation of another property (B) with "|" seperated values for each key (A).

For example:

Dim collectionA As A() = DAL.searchForComments

Here, "A" has got 50 properties (ID, Name, Address etc.) and "collectionA" has got multiple entries for "ID". What I want is to build a Dictionary (Of ID, Name) where all the "Name" values would be "|" seperated for each ID.

Any help would be appreciated.

Thanks very much.

Do you mean somewhat like this?:

Imports System.Linq

Public Class Properties
    Private Sub Properties_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim A1() As A = {New A(1, "name1", "address1"),
                            New A(2, "name2", "address2"),
                            New A(3, "name3", "address3")}
            Dim q = (From p In A1 Select p.ID, p.Address).ToDictionary(Function(x) x.ID, Function(x) x.Address)
        Catch ex As Exception

        End Try
    End Sub
End Class
Class A
    Public ID As Int32
    Public Name As String
    Public Address As String
    Public Sub New(Id As Int32, Name As String, Address As String)
        Me.ID = Id
        Me.Name = Name
        Me.Address = Address
    End Sub
End Class

Don't know what DAL.searchForComments stands for, but what about this(have not tested it)

var results = A().GroupBy(
    n => n.ID, 
    n => n.Name,
    (key, g) => new { ID = key, Name = g.ToList() });

@ddanbe why don't express lambda expressions in VB.NET instead of c#?

Six thousand rows doesn't seem so much, unless having constant calls to the function.

Imports System.Linq

Public Class Properties
    Private Sub Properties_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim A1() As A = {New A(1, "name1", "address1", "email1@email.com", "1234", "NY"),
                            New A(2, "name2", "address2", "email2@email.com", "5678", "LA"),
                            New A(3, "name3", "address3", "email3@email.com", "9012", "XX")}
            Dim q = (From p In A1 Select ID = p.ID,
                    value = p.Name + "|" + p.Address + "|" + p.email + "|" + p.phone + "|" + p.city).ToDictionary(
                    Function(x) x.ID, Function(x) x.value)
            Dim g = q
        Catch ex As Exception

        End Try
    End Sub
End Class
Class A
    Public ID As Int32
    Public Name As String
    Public Address As String
    Public email As String
    Public phone As String
    Public city As String
    Public Sub New(Id As Int32,
                   Name As String,
                   Address As String,
                   email As String,
                   phone As String,
                   city As String)
        Me.ID = Id
        Me.Name = Name
        Me.Address = Address
        Me.email = email
        Me.phone = phone
        Me.city = city
    End Sub
End Class

Oops xrj indeed I was wrong, neg rep me if you like, but I don't think (IMHO) that 'lambda' syntax in vb even looks like lambda syntax.
But I guess KM499 will have his way with my suggestion. If not, so be it. :)

I'm not gonna give a neg. rep. point, not even discuss what does lambda syntax look like in any specific language. Just that if this is a vb.net thread it's not a c# or not even asp.net. Isn't it?

commented: Completely right, and well said! +15

According to your description it looks to me that your collection will contain objects that may have more than one name associated with each specific id. If this is the case then something along these lines might be closer to what you're looking for:

Dim dict = (From a In collectionA
            Group a.Name By key = a.ID
                Into g = Group
            Select New With {.key = key, .value = String.Join("|", g)}).ToDictionary(Of String, String)(Function(x) x.key, Function(x) x.value)

This will create a Dictionary(Of String, String) with the key being an id value and the value is a string concatenated from different name values separated by |.

commented: Deep knowledge +15
commented: Exact and concise +6

Hi tinstaafl, ddanbe and xrj,

Thank you very much all for your suggestions.

xrj, this didn't work for me because as tinstaafl said, I mentioned earlier that my collection contains more than one record for each ID, so the following would throw exception of having a pre-existing key.

Dim q = (From p In A1 Select ID = p.ID,
                    value = p.Name + "|" + p.Address + "|" + p.email + "|" + p.phone + "|" + p.city).ToDictionary(
                    Function(x) x.ID, Function(x) x.value)

ddanbe and xrj, I understand that you tried to help me and really appreciate that. Thanks again.

tinstaafl, you simply rock and thanks very much indeed for your help on this as this is exactly I have been looking for. Cheers :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.