How to Export Data from Two Different Entities in LightSwitch


Again, one of my readers asked me that how to export data from two different entities [tables]. so I simply replied with an easy way to him that use a JOIN QUERY on the needed entities and use that to a screen and export the combined data.

So today I am going to explain how to export data from two different entities [tables] in LightSwitch.

Preparing the Solution:

Let fire up Visual Studio LightSwitch; create a project as show in the figure.


Follow the # numbers pointed in the figure to create the project.

Designing the LightSwitch Entities:

As per the need, we need to design three entities. Because the first two entities are the source entities to be combined and the third one is to insert the combined entity values.


Here, the SampleTableOne entity has the only one sample column to be exported.


The above figure shows the second entity to be combined.


The Export entity is used to have combined data from table one and two. So the required three tables are ready. Let us go ahead by designing the screen.

Designing the Screen:

To export the combined data, we need two screens. The first screen is used to have the first entity and the second one.


This is the main screen in which we have both SampleTableOne and SampleTableTwo entity. Create the List and Details Screen as shown in the figure.

Adding Entity into Screen as Data Item:

In the main screen we are going to have both entities so that we add the data for two entities in the same screen. So we need to add the second entity into the main screen.
To add the entity as Data Item, open the first screen and the top of the screen we can see the menu buttons. From that select "Add Data Item.." button.


The second entity SampleTableTwo is added as Query. In the above figure, we have selected the query option and add the query which returns all the records we have added.


Once we added both the entities, the ViewModel section shows the entities bonded with the first screen.

Export Screen:

Now we are ready to add the Export screen. we are going to bind the ExportData entity with the export screen as shown in the below figure.


As shown in the screen, add the Search Data Screen with the name ExportDataScreen.

The Code-behind – Combining Entities:

Once we are ready with screens we need to implement the logic to export the combined data.


To add the code for the export logic, we need to edit the entity Created method as shown in the above figure. Top of the ExportDataScreen, we will have the Write Code drop down like button. Hit the button and edit the method.

    partial void ExportDataScreen_Created()
// Getting all the records from both entities.

        IEnumerable<SampleTableOne> objSTO = this.DataWorkspace.ApplicationData.SampleTableOnes.GetQuery().Execute();

        IEnumerable<SampleTableTwo> objSTT = this.DataWorkspace.ApplicationData.SampleTableTwoes.GetQuery().Execute();
        // Inserting the first enties records into the Export entity.
        foreach (var item in objSTO)
            Export newRecord = Exports.AddNew();
            newRecord.ExportData = item.SampleDataOne;
        //Inserting the second entities records into the Export entity.
        foreach (var item in objSTT)
            Export newRecord = Exports.AddNew();
            newRecord.ExportData = item.SampleDataTwo;
        //Sorting the Export entity by the ExportData
        Exports.OrderBy(sort => sort.ExportData);

The above code inserts all the records of both the entities into the Export entity. We are ready to see the application in action. Hit F5.


In this article, we have seen about how to export data from two different entities in Visual Studio LightSwitch 2011.