467,104 Members | 1,201 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,104 developers. It's quick & easy.

Refer to LINQ Field name as Variable

LINQ works a lot like an Access DB Recordset in the way it functions, one of
the things I could do in Access is refer to the Fields thru a Variable as
below allowing me to Iterate over the Recordset with a test or slight change
to field names. Is there a way to Replicate this Method in LINQ, I've tried
setting the Variable as an Object "Dim tDayID AS Object = "tem.Day" & 1" but
it did not work?

IN ACCESS:
Dim DaI As String
DaI = "DayID" & 1

For da = 1 To 7
With RS1
If RS2.Fields(DaI) <0 Then
.Edit
Else
.AddNew
End If
!TimeValue = RS2.Fields("Day" & da)
!DayDate = dat
!Locked = False
!MemoNote = RS2.Fields("M" & da)
!LogID = RS2!GroupID
.Update
End With
Next da

Here is a snippet of the LINQ where I would like the tDayID to be the
Variable of LINQ Field and get its Value for testing (Code snippet not
Complete just a Sample):

Dim tDayID As Object
Dim dbTemp As New DataTempDataContext
Dim GetTemp = (From tem In dbTemp.tblTemps _
Where tem.EmployeeID = Me.HiddenEmplID.Value _
Select tem)

'Iterate over Temp Table records to Update Existing TimeRecord
For Each tem In GetTemp
tDayID = "tem.Day" & 1
da = 0

For da = 1 To 7 'Iterate over Temp Days to write Time Values to
Time Record
For Each ti In time 'Iterate over TimeRecord Table records
to find match

'HERE IS WHERE THE FIELD VARIABLE IS USED AND WOULD BE RESET DURING LOOP
If ti.DayID = tDayID Then
ti.ClientID = tem.ClientID
ti.ClientDesc = tem.Client
ti.LogID = tem.LogID
ti.PeriodID = tem.PeriodID
ti.ProjectDesc = tem.Project
ti.ProjectID = tem.ProjectID
ti.Task = tem.TaskID
ti.TaskDesc = tem.Task
ti.DayDate = dat
ti.TimeValue = tem.Day1
ti.Memo = tem.M1
ti.DayID = tDayID
End If
Next
Next da
Next
Jan 23 '08 #1
  • viewed: 4256
Share:
1 Reply
Hi John,

As for the Access variable feature you mentioned, I'm afraid the .NET LINQ
to SQL expression or engine can not directy support this. Because the LINQ
expression/query are parsed and compiled at compile time, and it should get
all the type information rather than change or determine them at
runtime(when executing). This is different from access which use VBA which
is a dynamically execution/late binding code.

therefore, for LINQ code, I would always suggest you use a property from a
definted data object.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "john" <no**@none.com>
Subject: Refer to LINQ Field name as Variable
Date: Wed, 23 Jan 2008 03:54:37 -0500
>
IN ACCESS:
Dim DaI As String
DaI = "DayID" & 1

For da = 1 To 7
With RS1
If RS2.Fields(DaI) <0 Then
.Edit
Else
.AddNew
End If
!TimeValue = RS2.Fields("Day" & da)
!DayDate = dat
!Locked = False
!MemoNote = RS2.Fields("M" & da)
!LogID = RS2!GroupID
.Update
End With
Next da

Here is a snippet of the LINQ where I would like the tDayID to be the
Variable of LINQ Field and get its Value for testing (Code snippet not
Complete just a Sample):

Dim tDayID As Object
Dim dbTemp As New DataTempDataContext
Dim GetTemp = (From tem In dbTemp.tblTemps _
Where tem.EmployeeID = Me.HiddenEmplID.Value _
Select tem)

'Iterate over Temp Table records to Update Existing TimeRecord
For Each tem In GetTemp
tDayID = "tem.Day" & 1
da = 0

For da = 1 To 7 'Iterate over Temp Days to write Time Values
to
>Time Record
For Each ti In time 'Iterate over TimeRecord Table records
to find match

'HERE IS WHERE THE FIELD VARIABLE IS USED AND WOULD BE RESET DURING
LOOP
If ti.DayID = tDayID Then
ti.ClientID = tem.ClientID
ti.ClientDesc = tem.Client
ti.LogID = tem.LogID
ti.PeriodID = tem.PeriodID
ti.ProjectDesc = tem.Project
ti.ProjectID = tem.ProjectID
ti.Task = tem.TaskID
ti.TaskDesc = tem.Task
ti.DayDate = dat
ti.TimeValue = tem.Day1
ti.Memo = tem.M1
ti.DayID = tDayID
End If
Next
Next da
Next
Jan 24 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by EDBrian | last post: by
7 posts views Thread by Andy B | last post: by
4 posts views Thread by =?Utf-8?B?Tmljaw==?= | last post: by
3 posts views Thread by Leo Seccia | last post: by
2 posts views Thread by timor.super@gmail.com | last post: by
2 posts views Thread by jelle79@gmail.com | last post: by
reply views Thread by kkrylov | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.