Kris1511
Kris1511

Reputation: 300

BETWEEN EQUIVALENT in LINQ

I am looking LINQ equivalent for the following query

Select * from ct_rate
WHERE
'2010-10-01 00:00:00'
BETWEEN start_date and end_date;

ANY IDEA?

Upvotes: 2

Views: 879

Answers (5)

castis
castis

Reputation: 8223

something like this?

var result = from context.ct_rate.Where(d => d.start_date >= "2010-10-01 00:00:00" && d.end_date <= "2010-10-01 00:00:00")

Upvotes: 0

Allfocus
Allfocus

Reputation: 293

I found this works when wanting only to compare the data part.

var results = from rate in ct_rates
              where rate.StartDate.Date <= date && rate.EndDate.Date >= date
              select rate;

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180948

Just use ordinary comparison operators

var result = ct_rates
    .Where(x => x.start_date <= myDate && x => x.endDate >= myDate);

Upvotes: 3

David North
David North

Reputation: 1406

In our case, Robert's answer was close to what I was looking for. In our data model, our 'end' or 'through' date columns are nullable.

Something like the following was needed:

Category.Where(
    w => w.ValidFrom <= now && 
   (w.ValidThru == null ? DateTime.MaxValue : w.ValidThru) >= now).Select(s => s).Dump();

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564931

You need to use two comparison operations:

DateTime date = new DateTime(2010, 10, 1);

var results = from rate in ct_rates
              where rate.StartDate <= date && rate.EndDate >= date
              select rate;

Upvotes: 7

Related Questions