SIGN UP MEMBER LOGIN:    
ARTICLE

How to Export Data from Two Different Entities in LightSwitch

Posted by Jaganathan Bantheswaran Articles | VS LightSwitch 2011 December 03, 2011
So today I am going to explain how to export data from two different entities [tables] in LightSwitch.
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 

You are welcome..

Posted by Jaganathan Bantheswaran Dec 13, 2011

Thx for the quick answer.

Posted by Jagith Khana Dec 13, 2011

Try like this, countOfTableRows1 = row count of a table which has more no of rows than the other table. countOfTableRows2 = row count of a table which has less no of rows than the other table for(int i=0;i<=countOfTableRows1; i++) { //Here add new row to table which is going to have the exported data. like exportdata.AddNew(); //Assign values from table 1 and table 2 using count values as index Break }

Posted by Jaganathan Bantheswaran Dec 13, 2011

Hi Jaganathan, that's a great instruction from you. But could you show me please how I can write the exportet Date in one line but in several columns? For example: my tables contains more than one column and datatypes: And I want to have in my ExportDataScreen: FirstName|Name| from SampleTableOne and ProductName and Quantity from SampleTableTwo like that: FirstName|Name|ProductName|Quantity| Thx in advance.

Posted by Jagith Khana Dec 12, 2011

Thank you...

Posted by Jaganathan Bantheswaran Dec 05, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor