Reputation: 6356
I have a query in the database that returns me a list of objects( in my case notifications), but that list brings a lot of repeated items(a notification can contain multiples attachments, so what the query does is a join with the attachment table, bringing back a lot of notificationid repeated, a notification with a lot of attachment). So what i want to acomplish is merge all the attachments for one specific notification id into one item (let say that my notification class has a list of attachments) I did this:
Dim dupes = list.GroupBy(Function(x) x.Notification.NotifiedID)
.Select(Function(s) s.ToList())
.ToList()
but i don't know how to merge the grouped ones, any help here?
Upvotes: 0
Views: 992
Reputation: 12780
After grouping, then select the first from each group:
var firstNotifiedItems = list.GroupBy(x=> x.Notification.NotifiedID).Select(g=> g.First()).ToList();
This will give you a single item for each notifiedID.
However, if you want all the items in a new object:
var notifiedItems = list.GroupBy(x=> x.Notification.NotifiedID).Select(g=> new{NotifiedID=g.First().NotifiedID, Items=g.ToList()}).ToList();
This will give you an object with the NotifiedID and a list of items for that ID.
Upvotes: 1
Reputation: 4187
Try this in c#
list.GroupBy(x=> x.Notification.NotifiedID).SelectMany(o=> o);
I don't know VB But you can try this
Dim dupes = list.GroupBy(Function(x) x.Notification.NotifiedID)
.SelectMany(Function(s) s)
.ToList()
Upvotes: 1