Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4547728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:07:52+00:00 2026-05-21T16:07:52+00:00

I have a list of data as shown below Original Data Genre Name Year

  • 0

I have a list of data as shown below

Original Data

Genre   Name                       Year 
Comedy  Shrek Forever After        2010
Drama   The Karate Kid             2010
Action  Iron Man 2                 2010
Action  Robin Hood                 2010
Drama   The Kids Are All Right     2010
Sci-Fi  Inception                  2010

and I need to group the movies by Genre and concatenate similar movies name separated by “,” so the new list looks like

Required Data

 Genre   Name                                     Year 
 Comedy  Shrek Forever After                      2010
 Drama   The Karate Kid, The Kids Are All Right   2010
 Action  Iron Man 2, Robin Hood                   2010
 Sci-Fi  Inception                                2010

The LINQ statement I wrote is as follows

Dim result = From p In movies Group p By p.Genre Into Group Select New Movie With {
         .Genre = Genre,
         .Name = String.Join(", ", Group.Select(Function(p) p.Name).ToArray()),
         .Year = Group.Select(Function(p) p.Year).FirstOrDefault
        }

Is there another way of doing it so that I don’t have to use “.Year = Group.Select(Function(p) p.Year).FirstOrDefault” or a different approach than mine.

Thanks

Sample Code:


Module Module1

    Sub Main()
        Dim movies As New List(Of Movie)
        movies.Add(New Movie("Comedy", "Shrek Forever After", 2010))
        movies.Add(New Movie("Drama", "The Karate Kid", 2010))
        movies.Add(New Movie("Action", "Iron Man 2", 2010))
        movies.Add(New Movie("Action", "Robin Hood", 2010))
        movies.Add(New Movie("Drama", "The Kids Are All Right", 2010))
        movies.Add(New Movie("Sci-Fi", "Inception", 2010))

        'Group amd Merge by genre
        Dim result = From p In movies Group p By p.Genre Into Group Select New Movie With {
         .Genre = Genre,
         .Name = String.Join(", ", Group.Select(Function(p) p.Name).ToArray()),
         .Year = Group.Select(Function(p) p.Year).FirstOrDefault
        }

        For Each r In result
            Console.WriteLine(r)
        Next
        Console.ReadKey()
    End Sub

    Public Class Movie
        Public Genre As String
        Public Name As String
        Public Year As Integer

        Public Sub New()
        End Sub

        Public Sub New(ByVal genre As String, ByVal name As String, ByVal year As Integer)
            Me.Year = year
            Me.Genre = genre
            Me.Name = name
        End Sub

        Public Overrides Function ToString() As String
            Return String.Format("Genre: {0}; Name:{1}; Year:{2}", Genre, Name, Year)
        End Function
    End Class

End Module
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:07:53+00:00Added an answer on May 21, 2026 at 4:07 pm

    If you’re sure that the Year will never be null, which based on your class it shouldn’t be, you could replace it with this:

    .Year = Group.First().Year
    

    However, I question your grouping approach. What happens when movies with the same genre don’t share the same year? One might be 2009, another 2010, and you’ll automatically take the first year. You could add an OrderBy to get the most recent year, but it all depends on your grouping intention.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list, which is need to be shown as data. But JTable
I have sales table data as shown below Sales Table Data http://lh5.ggpht.com/_KT7tmVVBHFM/TCryAax1JlI/AAAAAAAAAFk/zahMq4RoOuw/s144/Sales.png I want
I have a list of dictionaries as shown below. Each dictionary holds the total
I have JSON data like shown below. I only included column names once to
At the moment, with the code from below, I have the data shown like
I have already created a webpart to show the data from list, but I
I have a list of data points (0.2, 0.8, 0.95) that I want to
I have a list of data in javascript that looks like this: [[152, 48,
I have a list of data that is a schedule. Each item has a
I have a list of data of the form: [line1,a] [line2,c] [line3,b] I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.