Share via

Select random items from container

Bubba Jones 221 Reputation points
2022-08-01T08:40:47.633+00:00

I would like to select some documents from one of my containers. The code below compiles but fails on runtime - the Guid.NewGuid() part it used to work on EF core.

    FeedIterator<ForumPost> randomForumPosts =  
        container.GetItemLinqQueryable<ForumPost>().OrderBy(a => Guid.NewGuid()).Take(5).ToFeedIterator<ForumPost>();  

After doing some reading I found that there is no intrinsic way of performing a random selection in cosmos. However those posts were pretty dated. Is there a workaround for this or has there been any update since?

Azure Cosmos DB
Azure Cosmos DB

An Azure NoSQL database service for app development.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.