[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details

Linq:  How do I "seed" a table with dates using Visual Studio and LINQ

Asked by wilsonka63 in Visual Studio, Language Integrated Query - LINQ

Tags: Visual Studio, LINQ, Tables, Rows, Add, Fill in

I am trying to seed a table with dates in between two dates.  I did this in Access and I'm trying to figure out how to do it in Visual Studio 2008.  Here is the Access code:
'variables
Dim sql1, sql2, sql3 As String, s1, s2 As Recordset
Dim tag1, msg As String, i As Long
Dim d1, d2, d3 As Variant, title As String

Dim mydb As Database
Set mydb = Currentdb()

' prompt the user for dates

'startDate
d1 = DateAdd("d", -30, Now())
d1 = Format(d1, "mm/dd/yyyy")
msg = "Enter the beginning date ->"
title = "Get Bldg Dates"
d1 = CDate(InputBox(msg, title, d1))
'endDate
d2 = Format(Now(), "mm/dd/yyyy")
msg = "Enter the " & vbCrLf & "ending date ->"
title = "Get Bldg Dates"
d2 = CDate(InputBox(msg, title, d2))

' seed the table with days
sql1 = " SELECT * FROM [Table - Bldg 650];"
Set s1 = mydb.OpenRecordset(sql1, DB_OPEN_DYNASET, dbSeeChanges)

d3 = d1
Do While d3 <= d2
    s1.AddNew
    s1!Date = d3
    s1.Update
    d3 = DateAdd("d", 1, d3)
Loop

In VS2008, I am trying to eliminate the need for inputing a start and end date.  The code below reflects that change:

Imports System.Data.Linq

Public Class frmTransferData

    Dim systemData As New PFSystemDataContext
    Public dateNow As Date = Now().Date
    Public dateYesterday As Date
    Private Sub frmTransferData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim startDate = pckrStartDate.Value.ToString("yyyy-MM-dd 00:00:01")
        Dim endDate = pckrEndDate.Value.ToString("yyyy-MM-dd 11:59:59")

        Dim dateYesterday = dateNow.AddDays(-1).Date

        'open tbleBldg650 and get the last date

        Dim lastDate = From id In systemData.tblBldg650s _
                       Let selectDate = id.Date _
                       Order By selectDate Descending _
                       Select id.Date _
                       Take 1
        'add the date for each day in between the last date and dateYesterday

        Do While dateYesterday.ToString <= lastDate.ToString

            'add a new row to tblBldg650
            'the date column = lastdate +1
            'update
            'then add conseq dates until dateYesterday is reached
        Loop
    End Sub

I have tried several different ways with no success to include collecting the dates in between the two dates, putting them in a table and trying to force them into table by row.add etc.  

FYI - I "seed" this table with the dates, then I go back to the original table and take that date's data (240 records)  and average it, sticking that average in this "seeded" table for charting purposes.  

Any help would be GREATLY appreciated.  

[+][-]11/05/09 12:37 AM, ID: 25747618Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/05/09 06:09 AM, ID: 25749567Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/05/09 06:43 AM, ID: 25749915Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 - Hierarchy / EE_QW_3_20080625