locked
Display grid data in random order. RRS feed

  • Question

  • I have a data grid that lists various career sites. I just want them to display in a random order. I am sure this can be done in a LINQ query, but I am not sure how to accomplish it.

    partial void SitesRandom_PreprocessQuery(ref IQueryable<CareerSites> query)
            {
                query = from x in query
                        orderby //something Random
                        select x.SiteName;
            }
    Any help is appreciated.


    -----
    MrStu

    Friday, April 5, 2013 9:39 PM

Answers

  • Paul,

    Thanks for the reply... Had an epiphany this morning and figured it out. I needed to use order by Guid.NewGuid.

    I had two ideas, that could resolve this. First, I could have used a stored procedure using SQL NEWID() and called it via LINQ - think this is best if I was using a production db and not one I am creating. Second is LINQ as shown below.

    Both of these would be bad ideas to do for large datasets... I would guess performance would get slow.

    partial void SitesRandom_PreprocessQuery(ref IQueryable<CareerSites> query)
            {
                // Put career sites in a random order
                query = from x in query
                         orderby Guid.NewGuid() // set Guid to Random order by using NewGuid
                         select x;
            }


    -----
    MrStu

    • Proposed as answer by Paul Van Bladel Saturday, April 6, 2013 4:39 PM
    • Marked as answer by Stu Ryan Saturday, April 6, 2013 4:43 PM
    Saturday, April 6, 2013 4:08 PM

All replies

  • Nice question !

    I think you should add a column RandomSortValue and before retrieving the data you should walk through all records programmatically and assign a new Random value (Random.Next, you know).

    When displaying the data you should sort on the columns RandomSortValue.


    paul van bladel

    Saturday, April 6, 2013 5:43 AM
  • Paul,

    Thanks for the reply... Had an epiphany this morning and figured it out. I needed to use order by Guid.NewGuid.

    I had two ideas, that could resolve this. First, I could have used a stored procedure using SQL NEWID() and called it via LINQ - think this is best if I was using a production db and not one I am creating. Second is LINQ as shown below.

    Both of these would be bad ideas to do for large datasets... I would guess performance would get slow.

    partial void SitesRandom_PreprocessQuery(ref IQueryable<CareerSites> query)
            {
                // Put career sites in a random order
                query = from x in query
                         orderby Guid.NewGuid() // set Guid to Random order by using NewGuid
                         select x;
            }


    -----
    MrStu

    • Proposed as answer by Paul Van Bladel Saturday, April 6, 2013 4:39 PM
    • Marked as answer by Stu Ryan Saturday, April 6, 2013 4:43 PM
    Saturday, April 6, 2013 4:08 PM
  • If I had to do this, I'd do it in a custom RIA service. You'd also get to cull out any unnecessary table columns, which would be helpful if you have "large numbers" of records. You'd just add a long/int64 property & populate it with a random number. In LightSwitch (or in the service itself), you can sort by that property.

    I use a RIA service for anything that's "out of the ordinary". I LOVE what I can achieve with them.


    Yann - LightSwitch Central
    Luminous Tools for LightSwitch (a FREE productivity extension)
    FREE Themes, Controls, Types and Commands
     
    Please click "Mark as Answer", if any reply answers your question.
    Please click "Vote as Helpful", if you find any reply helpful.
     
    By doing this you'll help others to find answers faster.

    Monday, April 8, 2013 2:11 AM
    Moderator