开发者

Combining array of arrays into single, distinct array using LINQ

开发者 https://www.devze.com 2023-02-27 07:17 出处:网络
Can this be rewritten any better using LINQ? I\'m a C#er try开发者_开发问答ing to think in VB.NET for this current project. It\'s in an ASP.NET Web Forms .vb codebehind:

Can this be rewritten any better using LINQ? I'm a C#er try开发者_开发问答ing to think in VB.NET for this current project. It's in an ASP.NET Web Forms .vb codebehind:

Public ReadOnly Property AllowedCategoryIds As Integer()
    Get
        Dim ids = New List(Of Integer)

        For Each group In UserData.Current.AdGroups
            'group.CategoryIDs is Integer() type
            ids.AddRange(group.CategoryIDs)
        Next

        Return ids.Distinct()
    End Get
End Property


You can use SelectMany to flatten such a collection.

var array = arrayOfArrays.SelectMany(item => item).Distinct().ToArray(); // C#
Dim array = arrayOfArrays.SelectMany(Function(item) item).Distinct().ToArray() // VB
0

精彩评论

暂无评论...
验证码 换一张
取 消