none
Joining tables RRS feed

  • Question

  • Hi

    I'm trying to join tables using the entity framework

    ..It seems every example I look at makes sense until I try it and EF blows up and doesn't know what I am talking about.

    For example, say I have a SalesRegions table with a SalesRepID

    SalesRegions

    ---------------

    SalesRepID

    --

    1

    2

    3

    And a SalesRep table with an ID

    SalesRep

    ----------

    ID

    --

    5

    8

    9

    I tried joining them like the examples I looked at.  It fails.  I can't even try to guess what it wants.

                    var query = from sr in DM.SalesRegions 
                                   join srep in DM.SalesRep on srepl.ID equals sr.SalesRep
                                   select pd.FullName;

    It complains srep and sr are not in scope.  But following that sr is again in scope?  Weird.

    Of course it fails with out the modifyers as well.  I can load each table independantly.

    Can someone provide me with the proper syntax?  Thats all I need (I hope).  It can be really frustrating playing guessing games with code.  Its getting to the point where it would've been cheaper for me to write my own data manager, at least it would work!

    maybe I'm blaming EF for linq's faults, if so then I apologize EF, and darn you ling!

    Wednesday, August 31, 2016 9:26 PM

All replies

  • Hi MarDude,

    According to your description and related code, it seems that you want to do a join query and want to return a FullName collection, but I could not find 'srepl' and 'pd' defined on your code snippet. please modify your code like this:

    var query = from sr in DM.SalesRegions join srep in DM.SalesRep on srep.ID equals sr.SalesRepID select sr.FullName;

    // if FullName is in SalesRep table, please change sr to srep like : srep.FullName

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, September 1, 2016 2:17 AM
    Moderator
  • Thanks for your reply.  Sorry about the typos, I was simplifying things in my example.  With the corrections pointed out I still receive the error.  After reading the entire error it actually explained what to do.  I needed to swap the values in the on clause:

    var query = from sr in DM.SalesRegions 
                join srep in DM.SalesRep on sr.SalesRepID
    equals srep.ID             select sr.FullName;
    Thanks for your help!

    Friday, September 2, 2016 12:48 PM
  • I've written a join in LINQ to EF exactly never.

    You can follow your navigation properties like this:

    var q = from sr in DM.SalesRegion    
            select sr.SalesRep.FullName;
    David


    David http://blogs.msdn.com/b/dbrowne/

    Friday, September 2, 2016 4:28 PM