Cryptogear
Cryptogear

Reputation: 39

Linq request with BETWEEN

I'm trying to do a Linq request with two wheres, the problem is the syntax for the Between request. TransactionDateTime has this format "YYYY-MM-DD 12:48:19 252" My variables : strBeginnAbfr "2019-01-01", strEndeAbfr "2019-01-31"

I already tried another syntax I've found online, but it also didn't work.

 var abfrageGpNr = from x in db.Transactions
                                  where (x.GPNummer == strGPNrVar &&

                                   x.TransactionDateTime BETWEEN  strBeginnAbfr
                                 AND strEndeAbfr
                                 orderby x.TransactionDateTime 
                                  select x;

The result should be a list with Transactions with GPNr (1 user) and time horizon 1 month.

Upvotes: 1

Views: 90

Answers (4)

AAA
AAA

Reputation: 3670

You have to parse the strings to DateTime objects to compare them, which you do using standard comparison operators. Also observe how the let keyword is used:

var beginDate = DateTime.ParseExact(strBeginnAbfr, "yyyy-MM-dd", CultureInfo.InvariantCulture);
var endDate = DateTime.ParseExact(strEndeAbfr, "yyyy-MM-dd", CultureInfo.InvariantCulture);
var abfrageGpNr = from x in db.Transactions
                  let TxDate = x.TransactionDateTime 
                  where (x.GPNummer == strGPNrVar &&  
                  TxDate >= beginDate && TxDate <= endDate)
                  orderby TxDate 
                  select x;

Upvotes: 1

DarkRob
DarkRob

Reputation: 3833

There is no keyword between in LINQ,

Instead of this you need to do this in traditional way using >= and <=.

   var abfrageGpNr = from x in db.Transactions where (x.GPNummer == strGPNrVar 
            && (x.TransactionDateTime >=  strBeginnAbfr && x.TransactionDateTime <= strEndeAbfr) 
            orderby x.TransactionDateTime select x;

Upvotes: 0

cdev
cdev

Reputation: 5421

Use >= and <= to do comparison.

var abfrageGpNr = from x in db.Transactions
                where (x.GPNummer == strGPNrVar 
                && (x.TransactionDateTime >=  strBeginnAbfr && x.TransactionDateTime <= strEndeAbfr)
                orderby x.TransactionDateTime 
                select x;

Upvotes: 0

EylM
EylM

Reputation: 6113

 var abfrageGpNr = from x in db.Transactions
                                  where (x.GPNummer == strGPNrVar &&   
                                   x.TransactionDateTime >= strBeginnAbfr
                                 AND x.TransactionDateTime <= strEndeAbfr)
                                 orderby x.TransactionDateTime 
                                  select x;

Upvotes: 0

Related Questions