How to select random item from IEnumerable using LINQ

Forums LINQHow to select random item from IEnumerable using LINQ
Staff asked 2 years ago

I’m trying to come up a LINQ SELECT statement in C# to select random items from specific object instead of ordered items. how do i translate below syntax to random select

random = _dbase.OrderBy(x => x.company).Take(1000);

 

Answers (1)

Add Answer
Staff answered 2 years ago

var random = new Random();
random = _dbase.OrderBy(x => random.Next()).FirstOrDefault();

Subscribe

Select Categories