Entity Framework, LINQ: Select Data from tables, crosstables and parenttables

Posted on 2011-05-11
Last Modified: 2013-12-16
Hi experts

I'm new to this stuff.
I have the following model:
Cocktail 1--m RecipeEntries mc--1 Ingredients

Now I have a CocktailRepository and a Cocktail-Entity.
I need to get all my data for a cocktail, which includes of cource the recipe entries and the ingredients (the name of the ingredients).

But how can I do that, it's easy to select Cocktail and its related RecipeEntries but how do I get the Ingredients?
Should I even do that I one query or not?

I'm using EF4, .NET 4 and C#
Question by:Arikael
    LVL 18

    Expert Comment

    Please post the details about entities with foriegn keys/relationships.

    LVL 3

    Author Comment


    I attached a screenshot of the model (comments is not important) Model of the entities
    LVL 18

    Accepted Solution

    you do join
    like this sample:
    var t = from p in BlogBLL.Posts

                join cp in BlogBLL.CategoryPosts on p.PostId equals cp.PostId

                join c in BlogBLL.Categories on cp.CategoryID equals c.CategoryID

                select new


                    PostId = p.PostId,

                    CategoryName = c.CategoryName,

                    PostName = p.PostName,

                    PostSubName = p.PostSubName            


    LVL 3

    Author Comment

    thanks, so joins are the only way to do that?
    or does it even make sense to do it in one query?
    LVL 18

    Expert Comment

    yes 'joins'
    the sample very similar to your solution.
     EF4 will translate to SQL statement query that also joins...
    LVL 18

    Expert Comment

    check this too:
    EF4 LINQ Ordering Parent and all child collections with Eager Loading

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    728 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

    21 Experts available now in Live!

    Get 1:1 Help Now