Newb Linq DataContext Association Question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Newb Linq DataContext Association Question

  1. #1
    Join Date
    Oct 2003
    Posts
    133

    Newb Linq DataContext Association Question

    I'm trying to wrap my head around the LINQ to SQL dbml file and using it in my code

    If I add two tables

    Table1
    ID
    DESCRIPTION

    Table2
    FKID
    VALUE1
    VALUE2

    Then add in an association for the ID fields...should I then not be able to reference table2 in a linq query and get access to table1.DESCRIPTION?

    I just seem to be getting two classes made called Table1 and Table2...I was assuming the join happened in the dbml so I get the complete object in my code...not the case?

    Any info would be great,
    Thanks,
    Steve

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,981

    Re: Newb Linq DataContext Association Question

    Each statement gathers it's data when it's called the first time. LINQ doesn't care about db associations. Your DS doesn't even have to reflect the keys, as I removed the key from the db and the program still worked.

    This processes two tables (in the DS) and copies the new key to the new field in table2. I just moved the field in the db, and made it the new key.

    Code:
    w2.NewKey = curr
    Code:
        Public Sub RenumberBldgCode()
            '
            '  Need to update BOTH Bldgs & Avails !!!
            '
            Console.WriteLine("Renumbe BldgCode for Bldg and Avails")
            Console.WriteLine("Press y to continue")
            Dim ans = Console.ReadKey()
            If ans.KeyChar <> "y" Then
                End
            End If
            Console.WriteLine()
            Dim db As New theDStoUse ' the DataClass
            Dim q = From c In db.Bldgs _
                  Where c.BldgCode > 0 _
                  Select c.BldgCode, c.NewKey _
                  Order By BldgCode Ascending
    
            Dim w = From b In db.Avails _
                  Where b.AvailNo > 0 _
                  Select b.BldgCode, b.NewKey _
                  Order By BldgCode Ascending
            Dim found As Boolean = False
            Dim ctrNotFound As Integer = 999
            Dim ctr As Integer = 1
            Dim curr As Integer = 1
            For Each w1 In db.Bldgs
                curr = w1.NewKey
                found = False
                For Each w2 In db.Avails
                    If w2.BldgCode = w1.BldgCode Then
                        w2.NewKey = curr
                        found = True
                    End If
                Next
                curr += 1
            Next
            Console.WriteLine()
            Console.WriteLine("*** WORKING ***")
    
            '  comment out
            db.SubmitChanges()
            Console.WriteLine("Last BldgCode: " & curr)
            Console.WriteLine("press any key to Exit")
    
            '    Console.WriteLine(str)
            '    '        Console.WriteLine(q)
            '    Console.ReadKey()
        End Sub
    Last edited by dglienna; November 26th, 2008 at 06:37 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Oct 2003
    Posts
    133

    Re: Newb Linq DataContext Association Question

    Thanks for the reply, 1 more question then...

    Why create a DBML and link associations? I was always under the assumption that it's responsible for creating your objects to use in the from clause in the query so LINQ would handle the joins for me...



    Quote Originally Posted by dglienna View Post
    Each statement gathers it's data when it's called the first time. LINQ doesn't care about db associations. Your DS doesn't even have to reflect the keys, as I removed the key from the db and the program still worked.

    This processes two tables (in the DS) and copies the new key to the new field in table2. I just moved the field in the db, and made it the new key.

    Code:
    w2.NewKey = curr
    Code:
        Public Sub RenumberBldgCode()
            '
            '  Need to update BOTH Bldgs & Avails !!!
            '
            Console.WriteLine("Renumbe BldgCode for Bldg and Avails")
            Console.WriteLine("Press y to continue")
            Dim ans = Console.ReadKey()
            If ans.KeyChar <> "y" Then
                End
            End If
            Console.WriteLine()
            Dim db As New theDStoUse ' the DataClass
            Dim q = From c In db.Bldgs _
                  Where c.BldgCode > 0 _
                  Select c.BldgCode, c.NewKey _
                  Order By BldgCode Ascending
    
            Dim w = From b In db.Avails _
                  Where b.AvailNo > 0 _
                  Select b.BldgCode, b.NewKey _
                  Order By BldgCode Ascending
            Dim found As Boolean = False
            Dim ctrNotFound As Integer = 999
            Dim ctr As Integer = 1
            Dim curr As Integer = 1
            For Each w1 In db.Bldgs
                curr = w1.NewKey
                found = False
                For Each w2 In db.Avails
                    If w2.BldgCode = w1.BldgCode Then
                        w2.NewKey = curr
                        found = True
                    End If
                Next
                curr += 1
            Next
            Console.WriteLine()
            Console.WriteLine("*** WORKING ***")
    
            '  comment out
            db.SubmitChanges()
            Console.WriteLine("Last BldgCode: " & curr)
            Console.WriteLine("press any key to Exit")
    
            '    Console.WriteLine(str)
            '    '        Console.WriteLine(q)
            '    Console.ReadKey()
        End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center