I'm reading result from an json file inside the local project.it returns more than 4000 result.I want to get only random number of results (between 500- 1000) from that result.
var finalResultz = finalResults("All","All","All");//step one
in here it returns more than 4000 results.then I put into a list like this.
List<Results> searchOne = new List<Results>();//step two
foreach(var itms in finalResultz)
{
searchOne.Add(new Results
{
resultDestination = returnRegionName(itms.areaDestination),
mainImageurl = itms.testingImageUrl
});
}
ViewBag.requested = searchOne;
but I want to get only the results like I said.I want to resize the count in step one or in step two.how can I do that.hope your help.
You can use Random
class and Take()
method to extract N elements.
// create new instance of random class
Random rnd = new Random();
// get number of elements that will be retrieved from 500 to 1000
var elementsCount = rnd.Next(500, 1000);
// order source collection by random numbers and then take N elements:
var searchOne = finalResultz.OrderBy(x => rnd.Next()).Take(elementsCount);