How to get an array of random letters AZ?

Let me explain what I mean: I have a database that contains 4 columns, one of which is Letter, so each row has a character from 'A' to 'Z' and they are not unique, so there are several rows with "A", a few lines with "B", etc.

I want to make 26 lines (az) with all the letters, but randomize the lines that have the same letters. So I want 26 lines from A to Z, only one A, one B ..., and the lines of these letters are random. I hope you guys understand what I mean. Thanks in advance!

I thought something like:

var randomQuestions = questions.Distinct().GroupBy(q => q.Letter).Take(26).ToArray(); 

But I have no idea.

+5
source share
2 answers

If I understood the question correctly, something like this should work:

 Random random = new Random(); var randomQuestions = questions .GroupBy(q => q.Letter) .SelectMany(g => g.Skip(random.Next(g.Count())).Take(1)); 

Distinct() in your initial endeavors is at best useless and at worst counterproductive.

The above simply groups your data by letter and then selects a random individual item from each group. If you have twenty-six different letters in your source data, the above will select one random row of data for each of these different letters. You will end up with twenty-six items.

+9
source

I am going to suggest a slight variation on Peter.

Try the following:

 Random random = new Random(); var randomQuestions = from q in questions orderby random.Next() group q by q.Letter into gqs from gq in gqs.Take(1) select gq; 
0
source

Source: https://habr.com/ru/post/1269623/


All Articles