Solved

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

Posted on 2011-09-16
7
430 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 63

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 63

Accepted Solution

by:
Fernando Soto earned 500 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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 63

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 63

Expert Comment

by:Fernando Soto
ID: 36558123

Not a problem, glad to help.
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

680 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