mercredi 17 juin 2015

VB LINQ - Take one random row from each group

I'm trying to get one random row from each group of rows. I'm trying to use LINQ, but I'm not sure if that's the right approach. I'd like a Dictionary of Key/Name pairs.

My table is as such:

AnswerGroup     AnswerKey   AnswerName
---------------------------------------------
1               1           Yes
1               2           No
2               1           Never
2               2           A little bit
2               3           Mostly
2               4           Always
3               1           White
3               2           African American
3               3           Hispanic
3               4           Asian or Pacific Islander

For each AnswerGroup I need to choose a random Key/Name pair.

I have the beginnings of a LINQ query, but frankly I'm lost as I don't understand LINQ grouping and how to add an Enumerable.Take(1) to the group.

    Dim answerGroup As String = "AnswerGroup"
    Dim answerKey As String = "AnswerKey"
    Dim answerName As String = "AnswerName"

    Dim query = _
        From rows As DataRow In surveyAnswerKeys.Rows _
        Order By rows(answerGroup) _
        Group By questionSortKey = rows(answerGroup) _
        Into questionGroups = Group 

Any help would be appreciated. Thanks!




Aucun commentaire:

Enregistrer un commentaire