Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. confused on how to use linq to join 3 tables

confused on how to use linq to join 3 tables

Scheduled Pinned Locked Moved LINQ
databasecsharplinqtutorialquestion
5 Posts 5 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tonyonlinux
    wrote on last edited by
    #1

    I have an sql db with 3 tables like this Authors ----------- ID AuthorFirst AuthorLast BookAuthor (Serving as a Bridge) --------------------------------- AuthorID BookID Books ----------------------------------- ID Title Keywords Price Retired How would I select All the Authors and all the books associated with them using Linq? I though something like this but I'm very new to sql and Linq (a sight with good tutorials on linq and joins and all that good stuff would be great if you know one ) <pre> var lines =   from authors in db.Authors                                           join b   in db.Books on authors.ID equals b.ID                                           where authors.AuthorID == AuthorID                                           select authors;                                                 </pre> thanks

    D D H 3 Replies Last reply
    0
    • T tonyonlinux

      I have an sql db with 3 tables like this Authors ----------- ID AuthorFirst AuthorLast BookAuthor (Serving as a Bridge) --------------------------------- AuthorID BookID Books ----------------------------------- ID Title Keywords Price Retired How would I select All the Authors and all the books associated with them using Linq? I though something like this but I'm very new to sql and Linq (a sight with good tutorials on linq and joins and all that good stuff would be great if you know one ) <pre> var lines =   from authors in db.Authors                                           join b   in db.Books on authors.ID equals b.ID                                           where authors.AuthorID == AuthorID                                           select authors;                                                 </pre> thanks

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      For SQL: maybe this will help. You have there a simple and basic description alnog with examples for all of the most used "keywords"/commands including joins. ====== EDIT ====== As for LINQ this could help

      modified on Thursday, January 28, 2010 8:01 PM

      N 1 Reply Last reply
      0
      • D Dan Mos

        For SQL: maybe this will help. You have there a simple and basic description alnog with examples for all of the most used "keywords"/commands including joins. ====== EDIT ====== As for LINQ this could help

        modified on Thursday, January 28, 2010 8:01 PM

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        How the hell with this help? If you knew anything about Linq you would understand Linq query syntax IS NOT SQL syntax.


        I know the language. I've read a book. - _Madmatt

        1 Reply Last reply
        0
        • T tonyonlinux

          I have an sql db with 3 tables like this Authors ----------- ID AuthorFirst AuthorLast BookAuthor (Serving as a Bridge) --------------------------------- AuthorID BookID Books ----------------------------------- ID Title Keywords Price Retired How would I select All the Authors and all the books associated with them using Linq? I though something like this but I'm very new to sql and Linq (a sight with good tutorials on linq and joins and all that good stuff would be great if you know one ) <pre> var lines =   from authors in db.Authors                                           join b   in db.Books on authors.ID equals b.ID                                           where authors.AuthorID == AuthorID                                           select authors;                                                 </pre> thanks

          D Offline
          D Offline
          Daniel Grunwald
          wrote on last edited by
          #4

          You're thinking SQL-style. In LINQ, there's no need to use the join keyword to get an author's books. Instead, the generated classes have collection properties to represent relationships between the tables. AFAIK LINQ-to-SQL doesn't support N:M relations directly, but as in the SQL database, you can use N:1 and 1:M relations instead. To get one result row for each author-book combination, you can use multiple from clauses:

          var query = from a in db.Authors
          from b in a.BookAuthors
          select new { Author = a, Book = b.Book };

          Only a.BookAuthors is accessed in the nested from, not the whole set of book authors in the database. LINQ-to-SQL will use JOIN to express this query in SQL. These two posts present some nicer ways to handle N:M relations: http://blogs.msdn.com/mitsu/archive/2007/06/21/how-to-implement-a-many-to-many-relationship-using-linq-to-sql.aspx[^] http://blogs.msdn.com/mitsu/archive/2008/03/19/how-to-implement-a-many-to-many-relationship-using-linq-to-sql-part-ii-add-remove-support.aspx[^]

          1 Reply Last reply
          0
          • T tonyonlinux

            I have an sql db with 3 tables like this Authors ----------- ID AuthorFirst AuthorLast BookAuthor (Serving as a Bridge) --------------------------------- AuthorID BookID Books ----------------------------------- ID Title Keywords Price Retired How would I select All the Authors and all the books associated with them using Linq? I though something like this but I'm very new to sql and Linq (a sight with good tutorials on linq and joins and all that good stuff would be great if you know one ) <pre> var lines =   from authors in db.Authors                                           join b   in db.Books on authors.ID equals b.ID                                           where authors.AuthorID == AuthorID                                           select authors;                                                 </pre> thanks

            H Offline
            H Offline
            Hisham Abdullah Bin Ateya
            wrote on last edited by
            #5

            Hi dude .. take it easy!! you can accomplish your task with the following code:

            Dim db As New DbDataContext()
            Dim query=From a in db.Authors _
            From b in db.Books _
            From ba in db.BookAuthors _
            Where a.ID=ba.AuthorID And b.ID=ba.BookID _
            Select a,b

            .:: Wish to become better and better ::.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups