Solved

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

Posted on 2011-09-16
7
418 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 62

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 62

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 62

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 62

Expert Comment

by:Fernando Soto
ID: 36558123

Not a problem, glad to help.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now