?
Solved

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

Posted on 2010-09-21
3
Medium Priority
?
641 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 63

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

764 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