More in this section

Forums / Developing with Sitefinity / Fetch a Random list Item

Fetch a Random list Item

2 posts, 0 answered
  1. Karandeep Singh
    Karandeep Singh avatar
    12 posts
    Registered:
    27 May 2010
    20 Jul 2010
    Link to this post
    Hello,


    I have been implementing testimonials on a website using List controls. I want to fetch and display a random list item that should be different; every time a page is refreshed.  How can I create a control where i can fetch a random list item from a specific list. I am looking for a query or method that gives me back a single random list Item.



  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    21 Jul 2010
    Link to this post
    Hi Karandeep Singh,

    There is no built in method of ListManager that returns random results. You have to use ASP.NET Random class to get an unordered list

    sample1

    Random random = new Random((int)DateTime.Now.Ticks);
    rnd = IListOfListItems.ElementAt(random.Next(0, IListOfListItems.Count())).Headline;
     
    // if you use INamedList -  IListOfINamedList.ElementAt(random.Next(0, IListOfListItems.Count())).Name;

    sample 2

    Random rnd = new Random();
    var randomList = from item in ListItems
                     orderby rnd.Next()
                     select item;
    qList = randomList.ToList();
    return qList.GetRange(0, 10);


    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
2 posts, 0 answered