locked
Return Random Record In LightSwitch RRS feed

  • Question

  • Is there a way to have LightSwitch return a random record?

    For example, I have a list of about 50,000 contacts. I would like to have a screen return a random record based on a given set of criteria.

    Any help would be greatly appreciated

    Thanks!

    Monday, September 7, 2015 1:32 AM

Answers

All replies

  • This may help:

    Understanding The LightSwitch HTML Client Visual Collection

    You can keep calling something like this:

    screen.Customers.loadMore().then(function (result) {
    
    ...
    
    });
    Personally I would create a WCF RIA Service that only returned one random record.


    Unleash the Power - Get the LightSwitch HTML Client / SharePoint book

    http://LightSwitchHelpWebsite.com

    • Marked as answer by MCollazo1969 Tuesday, September 8, 2015 2:52 AM
    Monday, September 7, 2015 10:29 PM
  • Another option might be to:

    1. Create a Query over the Entity in question
    2. Implement a PreprocessQuery() method on that query that adds a where clause that randomly selects a single record based on your requirements (e.g. where Id equals a random value between the lowest and highest possible existing values)
    3. Call the above query from your client and only use the first retrned record (as there should only ever be one anyway)

    Hope that helps.


    Regards, Xander. My Blog

    Tuesday, September 8, 2015 1:05 AM
  • The problem is how to have all possible values without "running past the end"?

    Unleash the Power - Get the LightSwitch HTML Client / SharePoint book

    http://LightSwitchHelpWebsite.com

    Tuesday, September 8, 2015 1:12 AM
  • Thanks! I think I'm going the WCF RIA Service route....

    Tuesday, September 8, 2015 2:53 AM
  • Make a query in preprocessquery and use orderby Guid.NewGuid and then use select to pick one or more random results.

    Sven Elm

    Tuesday, September 8, 2015 9:35 PM
  • Yes indeed, Sven is correct, here is an example to return one random Client using the newly created RandomClient query:

    partial void RandomClient_PreprocessQuery(ref IQueryable<Client> query)
    {
      query = query.OrderBy(r => Guid.NewGuid()).Take(1);
    }

    In the _created() function of the view screen you can load the random Client like so:

    myapp.ViewRandomClient.created = function (screen) {
        screen.details.dataWorkspace.ApplicationData
            .RandomClient()
            .execute()
            .then(function onComplete(data) {
                screen.Client = data.results[0];
            });
    };


    Regards, Xander. My Blog

    • Edited by novascape Tuesday, September 8, 2015 10:12 PM
    Tuesday, September 8, 2015 10:08 PM