如何使用LINQ对数据列表进行分组和合并



我有一个如下所示的数据列表

原始数据

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

和我需要按类型对电影进行分组,并将相似的电影名称以"分隔,因此新列表看起来像

必需的数据

 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

我写的LINQ语句如下

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
        }

是否有其他方法,这样我就不必使用"。Year = Group.Select(Function(p) p.Year)。

感谢

示例代码:


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

如果您确定Year永远不会为空,根据您的类,它不应该为空,您可以将其替换为:

.Year = Group.First().Year

然而,我质疑你的分组方法。如果同一类型的电影不在同一年份,会发生什么?一个可能是2009年,另一个是2010年,你会自动选择第一年。您可以添加一个OrderBy来获得最近的年份,但这完全取决于您的分组意图。

相关内容

  • 没有找到相关文章

最新更新