-2

Possible Duplicate:
Is there a more efficent way to randomise a set of LINQ results?

I have a list having 300 records. I want to return only distinct, random records and only 50 records with LINQ:

myCollection = list.Distinct().Take(50).ToList(); // myCollection is the new list
Community
  • 1
  • 1
Ali Hasan
  • 1,045
  • 4
  • 16
  • 43

1 Answers1

3

You can order the list by random before Take(50). Should be like this:

myCollection = list.Distinct().OrderBy(s => Guid.NewGuid()).Take(50).ToList();

Idea of Guid.NewGuid() is not so efficient i think but you can consider using Random generator.

Kamil Lach
  • 4,519
  • 2
  • 19
  • 20