0

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!


Edit: I can expand the following query in the debugger to see an In Memory Query that produces a series of DataRows. When I hover over questionGroups it says it's a IEnumerable(Of Object). When I try to run that query into a list or DataTable I get error:

"Public member 'ToTable' on type 'WhereSelectEnumerableIterator(Of VB$AnonymousType_0(Of Object,IEnumerable(Of Object)),Object)' not found."

    Dim answerGroup As String = "QuestionSortKey"
    Dim answerNo As String = "AnswerNo"
    Dim surveyDefinitionNo As String = "Pk_SurveyDefinitionNo"

    Dim query = _
        From rows In surveyAnswerKeys.Rows _
        Where rows(answerNo) IsNot Nothing _
        Order By Guid.NewGuid() _
        Group By questionSortKey = rows(answerGroup) _
        Into questionGroups = Group _
        Select questionGroups.First()

    Dim randomAnswerNos As DataTable = query.ToTable

2 Answers 2

1

One quick way to shuffle items is to sort by a "random" value - Guid.NewGuid() usually works well enough. Then just pull the first row from each group:

Dim query = _
    From rows As DataRow In surveyAnswerKeys.Rows _
    Order By Guid.NewGuid() _
    Group By questionSortKey = rows(answerGroup) _
    Into questionGroups = Group _
    Select questionGroups.First() 
10
  • Rad. Now I just need to learn how to select just the two rows I need and put them in a Dictionary. Let me talk through this: Guid.NewGuid() adds a new column of randomly generated GUIDs and sorts all the columns. Group By questionSortKey defines the group and those groups are stored in questionGroups. And then we select the first row of each questionGroups? I think I got it. Thanks!
    – BClaydon
    Jun 17, 2015 at 18:42
  • Technically it doesn't add a new column - it just "associates" each row with a Guid and uses that value when sorting - conceptually you can think of it as adding a column but mechanically it's slightly different.
    – D Stanley
    Jun 17, 2015 at 19:01
  • GUID's are not defined to be random. Your only hoping they are. Jun 17, 2015 at 19:12
  • @MarcJohnston True, I'm relying on an implementation detail that Guid.NewGuid() creates a "random" Guid (meaning they are not in any easily discernible order). That seems to be good enough for most applications.
    – D Stanley
    Jun 17, 2015 at 19:32
  • True, I need a "good enough" as I'm creating sample data for testing.
    – BClaydon
    Jun 17, 2015 at 20:44
-2

Linq can't be used to pull a random row. I'd suggest you store all of the rows in a table and manually loop through each group. Then based on the number of rows in each group, generate a random number and pick that row. Only use LINQ to do the query and retrieve the results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.