Solved

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

Posted on 2011-09-16
7
443 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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