Reader Level:
ARTICLE

How to Export Data from Two Different Entities in LightSwitch

Posted by Jaganathan Bantheswaran Articles | LightSwitch 2012 December 03, 2011
So today I am going to explain how to export data from two different entities [tables] in LightSwitch.
  • 0
  • 0
  • 4834

Prologue:

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.

LightSwch1.gif

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.

LightSwch2.gif

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

LightSwch3.gif

The above figure shows the second entity to be combined.

LightSwch4.gif

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.

LightSwch5.gif

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.

LightSwch6.gif

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.

LightSwch7.gif

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.

LightSwch8.gif

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.

LightSwch9.gif

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.

Summary:

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

COMMENT USING