Refer to LINQ Field name as Variable

Refer to LINQ Field name as Variable

am 23.01.2008 09:54:37 von MD Websunlimited

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

RE: Refer to LINQ Field name as Variable

am 24.01.2008 04:34:06 von stcheng

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"
>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
>
>
>