Solved

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

Posted on 2011-09-16
7
416 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

16 Experts available now in Live!

Get 1:1 Help Now