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

时间:2011-04-19 14:36:29

标签: vb.net linq list

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

原始数据

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).FirstOrDefault”或者不同于我的方法。

谢谢

示例代码:


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 个答案:

答案 0 :(得分:2)

如果您确定Year永远不会为空(基于您的课程不应该为空),您可以将其替换为:

.Year = Group.First().Year

但是,我质疑你的分组方法。当具有相同类型的电影不在同一年共享时会发生什么?一个可能是2009年,另一个是2010年,你将自动进入第一年。您可以添加OrderBy来获取最近一年,但这一切都取决于您的分组意图。