Lightswitch load all data or run a async method synchronously RRS feed

  • Question

  • I have a grid with data in Lighswitch application. Grid has on every column posibility to filter column. Thanks to lsEnhancedTable

    Current problem: Loading a lot of records(10000) without paging turned on crashes Internet Explorer.

    Right now I am sending an ajax request to the web api controler with the list of ids of the Customers that I want to export. It works but with a lot of data it is very slow because I have to turn off the paging of the data to get all visible customers ids so I can iterate over the VisualCollection.

    To optimize this I would have to turn on back the paging of the data to 50 records so that the initial load is fast and move the loading of the data to a save/export to excel button.

    Possible solutions:

    • Load data all data on save button click. To do this I have to somehow load all items before I can iterate over collection.

    The code bellow locks UI thread since the loadMore is async. How to load all data synchronously? Ideally I would like to have some kind of progress view using a msls.showProgress.

            if (screen.tblCustomers.canLoadMore) {
    var visibleItemsIds = msls.iterate(screen.tblCustomers.data)
                           .where(function (c) {
                               return c;
    • Second approach would be turn on paging and pass just the filters applied by the users to the web api controller so I can query database and return only filtered records. But I don't know how to do that.

    • Third approach is the one that I am using right now. Turn off the paging->iterate over visual collection, get the customers id, pass them to the controller and return a filtered excel. This doesn't work well when there are a lot of records.

    • Iterate over filtered collection in the server side? I don't know if there is a way to do this in Lighswitch?

    Saturday, February 13, 2016 2:08 PM


  • This is a tricky one because of the way LS handles filters it's very difficult to do dynamic filters at runtime.

    Dale got around this in the enhanced table by using this hack on the client side collection loader query:

    LightSwitch-HTML-Client-Filter-Hack which takes advantage of the ability to inject an OData filter into the screen collection query.

    For performance, you definitely want to execute the filter server side. 

    So on the client side you have an OData filter string - you could pass that to the server in a query parameter, but then you'd have to deserialize it into Linq query predicates that LS can execute.

    This is similar to what the old Silverlight filter control did by sending an XML representation of the filter then parsed that into Linq predicates on the server.

    One thing you could do is implement this old unreleased Linq Dynamic Query Library which would allow you to send dynamic SQL-like Where clauses.  It extends the query.Where() method so you can do this in PreProcessQuery method:

    query = query.Where("CategoryID=1 AND State = "NY")

    At that point, you'd only need to translate your OData filter string from say this:

    "(CategoryID eq 1) AND (State eq 'NY')"

    Not a super easy task, but much simpler than doing your own predicate builders, etc.

    Alternatively, the cheap and easy way would be to hit the OData endpoint directly using the OData filter string from the enhanced table.  The issue here is you're by-passing the middle tier business logic.  If that's not a concern for you then go for it!  I'd suggest making your WebAPI accept an OData filter string then use that string to ajax the OData endpoint like


    That way the filter is passed as a string and the query to get your list of Customer IDs happens on the server.

    Sorry I'm not aware of an easier answer.



    • Edited by joshbooker Monday, February 15, 2016 11:24 PM
    • Proposed as answer by Weiwei CaiModerator Friday, February 19, 2016 8:52 AM
    • Marked as answer by Angie Xu Wednesday, February 24, 2016 12:37 AM
    Monday, February 15, 2016 11:15 PM