locked
Run a query at Application start-up

    Question

  • I would like to know where I can put the code to run a query against an entity to perfom some cleaning before the application is loaded.

    Thanks


    Rachida Dukes

    Monday, May 21, 2012 1:05 AM

Answers

  • You haven't initialized the myOrder local variable to anything. Thus, there is nothing to iterate (in your for loop). You need to get results from a query on the data workspace and iterate over that:

    DataWorkspace dataWorkspace = this.CreateDataWorkspace();
    IDataServiceQueryable<Order> query = dataWorkspace.ApplicationData.Orders.GetQuery();
    IEnumerable<Order> orders = query.Execute(); // Expand on this to get the subset of orders you want.
    
    foreach (Order order in orders)
    {
        // Do something on each order.
    }


    Justin Anderson, LightSwitch Development Team

    Monday, May 21, 2012 4:51 AM
    Moderator

All replies

  • Hi

    Open your Application.cs file from Client Project. Write the below code.

    partial void Application_Initialize()
            {
                //Your code here
            }

    This code will fire before your application initialize.

    Hope this help...


    Rashmi Ranjan Panigrahi


    Monday, May 21, 2012 1:10 AM
  • Wow Rashmi, that was so fast, thanks for your quick response.

    I found it and I wrote this code, but it didn't work

              DataWorkspace myDataWorkspace = this.CreateDataWorkspace();
                IEnumerable<Order> myOrder;

                foreach (var mOrdersItem in myOrder)
                {
                   //my codes

                   
                } // end of the loop

    Can you tell me what I'm doing wrong?

    Thanks again,

    Rachida


    Rachida Dukes

    Monday, May 21, 2012 1:36 AM
  • Hi Rachida

    Check each and every piece of your code by putting break point. In my case Application_Initialize() method is working fine. You have to write your code within Application_Initialize() method.

    Please follow the below threads for more details

    http://social.msdn.microsoft.com/Forums/en-US/lightswitch/thread/b780be92-c86d-4240-9913-b9cde3429090

    http://social.msdn.microsoft.com/Forums/en-AU/lightswitch/thread/f5c188ad-98e9-4349-875b-5577d072a84d

    Regards


    Rashmi Ranjan Panigrahi


    • Edited by babloo1436 Monday, May 21, 2012 3:16 AM
    Monday, May 21, 2012 3:13 AM
  • You haven't initialized the myOrder local variable to anything. Thus, there is nothing to iterate (in your for loop). You need to get results from a query on the data workspace and iterate over that:

    DataWorkspace dataWorkspace = this.CreateDataWorkspace();
    IDataServiceQueryable<Order> query = dataWorkspace.ApplicationData.Orders.GetQuery();
    IEnumerable<Order> orders = query.Execute(); // Expand on this to get the subset of orders you want.
    
    foreach (Order order in orders)
    {
        // Do something on each order.
    }


    Justin Anderson, LightSwitch Development Team

    Monday, May 21, 2012 4:51 AM
    Moderator
  • Thanks Justin, it works just fine. I had no idea how to get started.

    Only one little issue, I noticed that lightswitch is running very slow for the first three lines of codes before the loop.

    I hope this process would run faster in the finale version.

    Thanks again, you were very helful.

    Rachida


    Rachida Dukes

    Monday, May 21, 2012 11:21 PM