Grant Thomas
Grant Thomas

Reputation: 45083

Split list into sets using Linq

There are plenty of seemingly related Linq questions, however a quick browse through the content doesn't seem to answer my question specifically enough for my limited intelligence to grasp.

We have a table named PropertyInteractions in which is stored threaded messages via a thread id, the thread identifier is the Id of the initial interaction record. Given the following Linq query (which retrieves all interactions concerning a user), how might I split interactions into lists of PropertyInteractions grouped by Id?

Dim interactions = (From interaction In propertyItem.PropertyInteractions _
                    Where (interaction.SenderId = CurrentUser.ID OrElse _
                           interaction.RecipientId = CurrentUser.ID) AndAlso _
                           interaction.InteractionType = InteractionType.ViewRequest _
                    Order By interaction.ThreadId _
                    Select interaction)

EDIT:

Given Jon's input, this is what I have come to at the moment, although it may be subject to change...

Dim interactions = _
    (From interaction In propertyItem.PropertyInteractions _
     Where (interaction.SenderId = CurrentUser.ID OrElse _
            interaction.RecipientId = CurrentUser.ID) AndAlso _
            interaction.InteractionType = InteractionType.ViewRequest _
     Order By interaction.ThreadId _
     Group interaction By interaction.ThreadId Into Group)

Upvotes: 1

Views: 741

Answers (2)

Peter Lillevold
Peter Lillevold

Reputation: 33930

Dim groupedInteractions = interactions.GroupBy(Function(i) i.Id)

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1500825

Sounds like you just want:

Group interaction By interaction.Id Into Group

See the docs for the Group By clause for more information. (It looks like it works subtly differently from the similar C# query expression syntax; VB experts may be around to give more detailed advice.)

Upvotes: 4

Related Questions