How do you create a relational object using the LinqDataSource

Posted on 2009-04-19
Last Modified: 2013-12-17
I am trying to use the LinqDataSource in an application.  I have two tables that have a relation between each other through a field call MPI.  I have setup the DataClass.dbml with the two tables and created an association between the two tables.  I had expected that after I did that I could then see all fields from both tables when I dragged the LinqDataSource to the canvas.  When I configured the LinqDataSource I only get the fields from the table that is selected.  How can I access fields from both tables when using the LinqDataSource?
Question by:kwh3856
    LVL 11

    Expert Comment

    The selected entity will have a property which represents a collection of the related table instances.
    For example, if I have a Sample table and a SampleUnt table which refers to the Sample Table, I could query:

    SampleTrackDataContext stdc = new SampleTrackDataContext();
    stdc.Log = new DebuggerWriter();
    var samples = from s in stdc.Samples
                  select s;
    foreach (Sample s in samples) {
        foreach (SampleUnit su in s.SampleUnit) {

    Open in new window

    LVL 62

    Expert Comment

    by:Fernando Soto
    Hi Kenny;

    Can you post the DBML file and and the code file it created so I can see your DataContext?

    LVL 21

    Accepted Solution

    Relations are already there, you just have to look at them.
    For example, if yoiu have a GridView and a LDS that looks at table 'aaa' and it is related to table 'bbb' and you want to see the column 'some_column' in 'bbb' and it is related through the column 'MPI' you could access it like this:

      <%# Eval("column_in_aaa") %> - it will eval columns in aaa normally
      <%# Eval("bbb.some_column") %> - if you want stuff in bbb, you will have to specify it

    Open in new window


    Author Closing Comment

    Thank you very much.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my 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…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    759 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

    14 Experts available now in Live!

    Get 1:1 Help Now