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. how would one convert this sql to linq to sql ? [Solved]

how would one convert this sql to linq to sql ? [Solved]

Scheduled Pinned Locked Moved LINQ
databasecsharplinqhelpquestion
5 Posts 3 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

    is there a free converter anywhere? How would I convert this to linq to sql query?

    SELECT Book.*, Authors.*,BookNumber.*
    FROM Book INNER JOIN
    (Authors INNER JOIN BookAuthors ON Authors.ID = BookAuthors.AuthorID)
    ON Book.ID = BookAuthors.BookID INNER JOIN
    BookNumber ON Book.ID = BookNumber.BookID

    WHERE AuthorFirst = @AuthorFirst AND AuthorLast = @AuthorLast;

    var query = from b in db.Books
    join ba in db.BookAuthors on b.ID equals ba.BookID
    join a in db.Authors on ba.AuthorID equals a.ID
    join bn in db.BookNumbers on ba.BookID equals bn.BookID
    where a.AuthorFirst == splitstring[1].ToString() && a.AuthorLast == splitstring[0].ToString()
    select b;

    in the linq i posted everything seems to join except the booknumber table any help would be great. thanks.

    modified on Thursday, February 11, 2010 12:13 AM

    N P 2 Replies Last reply
    0
    • T tonyonlinux

      is there a free converter anywhere? How would I convert this to linq to sql query?

      SELECT Book.*, Authors.*,BookNumber.*
      FROM Book INNER JOIN
      (Authors INNER JOIN BookAuthors ON Authors.ID = BookAuthors.AuthorID)
      ON Book.ID = BookAuthors.BookID INNER JOIN
      BookNumber ON Book.ID = BookNumber.BookID

      WHERE AuthorFirst = @AuthorFirst AND AuthorLast = @AuthorLast;

      var query = from b in db.Books
      join ba in db.BookAuthors on b.ID equals ba.BookID
      join a in db.Authors on ba.AuthorID equals a.ID
      join bn in db.BookNumbers on ba.BookID equals bn.BookID
      where a.AuthorFirst == splitstring[1].ToString() && a.AuthorLast == splitstring[0].ToString()
      select b;

      in the linq i posted everything seems to join except the booknumber table any help would be great. thanks.

      modified on Thursday, February 11, 2010 12:13 AM

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      I'd suggest you to take a look at SQL tracer in SQL server and see the SQL statement executed. This will give an idea about how joins are formed.

      Best wishes, Navaneeth

      1 Reply Last reply
      0
      • T tonyonlinux

        is there a free converter anywhere? How would I convert this to linq to sql query?

        SELECT Book.*, Authors.*,BookNumber.*
        FROM Book INNER JOIN
        (Authors INNER JOIN BookAuthors ON Authors.ID = BookAuthors.AuthorID)
        ON Book.ID = BookAuthors.BookID INNER JOIN
        BookNumber ON Book.ID = BookNumber.BookID

        WHERE AuthorFirst = @AuthorFirst AND AuthorLast = @AuthorLast;

        var query = from b in db.Books
        join ba in db.BookAuthors on b.ID equals ba.BookID
        join a in db.Authors on ba.AuthorID equals a.ID
        join bn in db.BookNumbers on ba.BookID equals bn.BookID
        where a.AuthorFirst == splitstring[1].ToString() && a.AuthorLast == splitstring[0].ToString()
        select b;

        in the linq i posted everything seems to join except the booknumber table any help would be great. thanks.

        modified on Thursday, February 11, 2010 12:13 AM

        P Offline
        P Offline
        PunkIsNotDead
        wrote on last edited by
        #3

        Hi tonionlinux! Shuldn't you use in 5th line

        join bn in db.BookNumbers on b.ID equals bn.BookID

        :confused: good luck ;)

        T 1 Reply Last reply
        0
        • P PunkIsNotDead

          Hi tonionlinux! Shuldn't you use in 5th line

          join bn in db.BookNumbers on b.ID equals bn.BookID

          :confused: good luck ;)

          T Offline
          T Offline
          tonyonlinux
          wrote on last edited by
          #4

          Thanks I changed that but for some reason i only get the first surrogate number. even though when i look in the debugger everything is there. Hmmm.

              private void treeView1\_AfterSelect(object sender, TreeViewEventArgs e)
              {
                  try
                  {
                      toolStripStatusLabel1.Text = "DATABASE IS CURRENTLY FILTERED PLEASE HIT REFRESH BUTTON IN TOP RIGHT HAND CORNER TO UNFILTER";
                      
                      using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
                      {
                          
                          if (treeView1.SelectedNode.Text == "Authors")
                          { return; }
                          else
                          {
          
          
                              String\[\] splitstring = null;
                              string treeviewtext = treeView1.SelectedNode.Text.Trim();
                              splitstring = treeviewtext.Split(',');
          
                              var query = from b in db.Books
                                          join ba in db.BookAuthors on b.ID equals ba.BookID
                                          join a in db.Authors on ba.AuthorID equals a.ID
                                          join bn in db.BookNumbers on b.ID equals bn.BookID
                                          where a.AuthorFirst == splitstring\[1\].ToString() && a.AuthorLast == splitstring\[0\].ToString()
                                          select b;
          
                              //var query =
                              //             from a in db.Authors
                              //             join ba in db.BookAuthors on a.ID equals ba.AuthorID
                              //             join b in db.Books on ba.BookID equals b.ID
                              //             join bn in db.BookNumbers on b.ID equals bn.BookID
                              //             where a.AuthorFirst == splitstring\[1\].ToString() && a.AuthorLast == splitstring\[0\].ToString()
                              //             select a;
          
          
                              bookBindingSource.DataSource = query.ToList();
                              bindingNavigator1.BindingSource.DataSource = bookBindingSource.DataSource;
          
                          }
                        
                          
                      }
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.ToString());
                  }
          
                  
              }
          

          Here is some info on my database. I have 4 Tables Authors, Books, BookAuthor, Booknumbers Author --------

          T 1 Reply Last reply
          0
          • T tonyonlinux

            Thanks I changed that but for some reason i only get the first surrogate number. even though when i look in the debugger everything is there. Hmmm.

                private void treeView1\_AfterSelect(object sender, TreeViewEventArgs e)
                {
                    try
                    {
                        toolStripStatusLabel1.Text = "DATABASE IS CURRENTLY FILTERED PLEASE HIT REFRESH BUTTON IN TOP RIGHT HAND CORNER TO UNFILTER";
                        
                        using (var db = new mombooksDataContext(Properties.Settings.Default.BooksConnectionString2))
                        {
                            
                            if (treeView1.SelectedNode.Text == "Authors")
                            { return; }
                            else
                            {
            
            
                                String\[\] splitstring = null;
                                string treeviewtext = treeView1.SelectedNode.Text.Trim();
                                splitstring = treeviewtext.Split(',');
            
                                var query = from b in db.Books
                                            join ba in db.BookAuthors on b.ID equals ba.BookID
                                            join a in db.Authors on ba.AuthorID equals a.ID
                                            join bn in db.BookNumbers on b.ID equals bn.BookID
                                            where a.AuthorFirst == splitstring\[1\].ToString() && a.AuthorLast == splitstring\[0\].ToString()
                                            select b;
            
                                //var query =
                                //             from a in db.Authors
                                //             join ba in db.BookAuthors on a.ID equals ba.AuthorID
                                //             join b in db.Books on ba.BookID equals b.ID
                                //             join bn in db.BookNumbers on b.ID equals bn.BookID
                                //             where a.AuthorFirst == splitstring\[1\].ToString() && a.AuthorLast == splitstring\[0\].ToString()
                                //             select a;
            
            
                                bookBindingSource.DataSource = query.ToList();
                                bindingNavigator1.BindingSource.DataSource = bookBindingSource.DataSource;
            
                            }
                          
                            
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
            
                    
                }
            

            Here is some info on my database. I have 4 Tables Authors, Books, BookAuthor, Booknumbers Author --------

            T Offline
            T Offline
            tonyonlinux
            wrote on last edited by
            #5

            I figured it out I had to add

            var options = new DataLoadOptions();
            options.LoadWith<Book>(c => c.BookAuthors);
            options.LoadWith<BookAuthor>(c => c.Author);
            options.LoadWith<Book>(c => c.BookNumbers);

                            db.LoadOptions = options;
            

            forgot about that but it works now :)

            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