Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

.NET Entity Framwork and the need to "join" data

Posted on 2011-09-16
7
Medium Priority
?
458 Views
Last Modified: 2012-05-12
I have a database that has the following tables, among others:
Lab
LabFunction
LabFunctionMap
These are shown in the image.  In the next image, you will see I added an Entity Framework model to my Visual Studio solution.  The Entity Framework datasource shows the jqLabs available to me as circled in green.  You also see the jqLabFunctions available in the in the datasource tree as a "child" class in the jqLabs entry, then you see jqLabs under that.  This is the resulting Entity Framework generated from the database.  This is all driven by the fact that I map the Lab and the LabFunction via the LabFunctionMap table.

My question is, how do I write the code to use the Entity Framework datasource and load the jqLabs into a Master datagridview and the jqLabFunctions into a Detail datagridview.
 Database View View from within Visual Studio 2010
0
Comment
Question by:chrisog
  • 4
  • 2
7 Comments
 
LVL 14

Expert Comment

by:binaryevo
ID: 36554028
In your context, don't use the join keyword but another from statement:

var items = from t in table1
                    from j in table2
                    where t.field1 ==
                               j.field1
                     select new
                     {
                           objectval1 = t.field2,
                           objectval2 = j.field2
                     };

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 36554829
Hi chrisog;

The best way to do a Master/Detail with Linq to Entity Framework is to let the IDE environment build the underneath plumbing for you. Using your project data source to step you through the way.

1. Add a Linq to Entity Framework model to your project. 
2. Once the designer is displayed add the tables that the project will be using.
3. Save and compile the project.
4. Add a DataSource object to your project By clicking on the Main Menu -> Data -> Add New DataSource.
5. In the DataSource Window select Object in the box marked "Where will the application get data from?" and click on the Next button.
6. Expand the Project Name node and then Expand the node with the Project Name again. 
7. Now select the Master table name, in this case it is jqLabs.
8. Click on the Finish button.
9. Open the DataSource window if it is not already displayed.
10. Expand the jqLabs table and then select jqLabs table. Once selected it will turn into a dropdown box. Open it and select DataGridView.
11. Now drag the jqLabs table on to the form. This will add one BindingSource component named jqLabsBindingSource and a BindingNavigator called jqLabsBindingNavigator as well as the Master DataGridView with all plumbing connected for you.
12. Now drag the jqLabFunctions table on to the form from the DataSource window making sure you selected DataGridView from the dropdown box. This will add a BindingSource component called jqLabFunctionsBindingSource to the form and the Details DataGrridView.
13. If you do not wish to have the BindingNavigator on the form then select the BindingNavigator control and delete it.
14. Now query the data source as shown in the code snippet below and that is it.
15. Run the project.

Open in new window


var FunctionQuery = from l in dsLabs.Include("jqLabFunctions")
                    select l;

Open in new window

                   

Fernando
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 36554852
Hi chrisog;

After you complete the steps through 14 in my last post there will be one more thing you will need to do to make this complete and that is to remove ALL the columns in both DataGridViews that reference a collection from another table. So select a DataGridView and go to the properties windows and select the Columns property and click on the button on the far right to open the Edit Columns dialog box and select each column you do not want to appear in the DataGridView and delete them. Then press OK button. Do the same for the other grid.

Fernando
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:chrisog
ID: 36556909
Fernando,
Thanks so much, but when you say "1. Add a Linq to Entity Framework model to your project. "; ...what do you really mean?  Can you walk me through those steps?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 36557047
Hi chrisog;

You stated the following, "I added an Entity Framework model to my Visual Studio solution.", when you did this you actually completed steps 1 through 3.

Fernando
0
 

Author Closing Comment

by:chrisog
ID: 36558108
Thanks!
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 36558123

Not a problem, glad to help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question