[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Example for LINQ to Entities query with multi level relationship (grandparent / grandchild records)

Posted on 2008-11-14
Medium Priority
Last Modified: 2013-11-11
Hi All,
Hoping there's a good LINQ coder that can help me out here. My database has a string of related tables where table B has a foreign key to table A and table C has foreign key to table B and table D has foreign key to table C. I need to write a LINQ to Entities query that looks something like:

var query = from d in entities.D where d.c.b.a.primarykey = x select d.textfield;

I don't seem to have grip on how to reach that far through the relationships to filter on a value.

Question by:danburyit
LVL 64

Expert Comment

by:Fernando Soto
ID: 22964049
Can you provide a UML type diagram of how the tables are set up Primary Key / Foreign Key and Relationships to see if I may be able to help you.

Accepted Solution

danburyit earned 0 total points
ID: 22967411
I found a solution. (I'll post in case it helps anyone else. I'm just starting to get the hang of this LINQ stuff.) I changed object names to make it simple and generic (and match my question.)

Just to reiterate a bit: A table has a child table B (B has foreign keys to A). B table has a child table C. C has a child table D. (It's just a 4 table parent/child heirarchy chain.) I need to return a distinct list of D.PrimaryKey and D.TextField values where A.PrimaryKey = x.

(Thanks for looking)
            using (myEntities ent = new myEntities())
                ObjectQuery<Dtable> dTable = ent.Dtable;
                ObjectQuery<Ctable> cTable = ent.Ctable;
                var query = (from d in dTable
                             from c in cTable
                             where d.DprimaryKey == c.d.DprimaryKey &&
                             c.b.a.AprimaryKey == x &&
                             d.TextField.Length > 0
                             select new { Key = d.DprimaryKey, Value = d.TextField }).Distinct();

Open in new window


Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

873 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