Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I search between dates (variables) using Visual Basic Linq to sql?

Posted on 2010-09-21
3
Medium Priority
?
644 Views
Last Modified: 2013-11-11
I have two dates stored as strings:
dim x as string = '2010/9/9'
dim y as string = '2010/8/8'

I have the following LINQ TO SQL statement that works perfectly fine:
            Dim listTrans As List(Of TransactionData) = _
                (From trans In db.TransHeaderTables _
                Select New TransactionData With _
                    { _
                        .TransId = trans.TransId.ToString, _
                        .EncryptionTCN = trans.TCN, _
                        .EncryptionORI = trans.EncryptionORI _
                    }).ToList()

What I need is to add in a way to do a WHERE trans.CompletedDate between X and Y.  Can someone modify the statement above?  Also, I'd love a separate modified statement above that can grab the records within the last X days.

Thank you!
0
Comment
Question by:RichardRiga
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 33728131

' Between X and Y
Dim listTrans As List(Of TransactionData) = _
    (From trans In db.TransHeaderTables _
     Where trans.CompletedDate >= Convert.ToDateTime(x) AndAlso trans.CompletedDate <= Convert.ToDateTime(y)
    Select New TransactionData With _
        { _
            .TransId = trans.TransId.ToString, _
            .EncryptionTCN = trans.TCN, _
            .EncryptionORI = trans.EncryptionORI _
        }).ToList()

' Last X days
Dim listTrans As List(Of TransactionData) = _
    (From trans In db.TransHeaderTables _
     Where trans.CompletedDate >= Now.AddDays(x * -1)
    Select New TransactionData With _
        { _
            .TransId = trans.TransId.ToString, _
            .EncryptionTCN = trans.TCN, _
            .EncryptionORI = trans.EncryptionORI _
        }).ToList()

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 33728491
Something like this
' --- Request 1 ---
Dim x as DateTime = Date.Parse("2010/9/9")
Dim y as DateTime = Date.Parse("2010/8/8")

Dim listTrans As List(Of TransactionData) = _
    (From trans In db.TransHeaderTables _
     Where trans.CompletedDate >= y  And trans.CompletedDate <= x
     Select New TransactionData With _
     { _
            .TransId = trans.TransId.ToString, _
            .EncryptionTCN = trans.TCN, _
            .EncryptionORI = trans.EncryptionORI _
     }).ToList()

' --- Request 2 ---     
Dim NumberOdDays As Integer = -5     
Dim z1 As DateTime = DateTime.Today
Dim z2 As DateTime = DateTime.Today.AddDays(NumberOdDays)     
     
Dim listTrans As List(Of TransactionData) = _
    (From trans In db.TransHeaderTables _
     Where trans.CompletedDate >= z2  And trans.CompletedDate <= z1
     Select New TransactionData With _
     { _
            .TransId = trans.TransId.ToString, _
            .EncryptionTCN = trans.TCN, _
            .EncryptionORI = trans.EncryptionORI _
     }).ToList()
     

Open in new window

0
 
LVL 1

Author Closing Comment

by:RichardRiga
ID: 33745436
Perfect.  Thanks so much.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question