-
November 26th, 2008, 03:33 PM
#1
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
-
November 26th, 2008, 07:35 PM
#2
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:
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 07:37 PM.
-
November 27th, 2008, 11:38 PM
#3
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...
Originally Posted by dglienna
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|