Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

.net - Combining array of arrays into single, distinct array using LINQ

Can this be rewritten any better using LINQ? I'm a C#er trying 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...