LINQ中的等价关系

LINQ中的等价关系,linq,Linq,我正在为下面的查询寻找LINQ等价物 Select * from ct_rate WHERE '2010-10-01 00:00:00' BETWEEN start_date and end_date; 有什么想法吗?您需要使用两种比较操作: DateTime date = new DateTime(2010, 10, 1); var results = from rate in ct_rates where rate.StartDate <= date &

我正在为下面的查询寻找LINQ等价物

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

有什么想法吗?

您需要使用两种比较操作:

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

var results = from rate in ct_rates
              where rate.StartDate <= date && rate.EndDate >= date
              select rate;
DateTime日期=新的日期时间(2010,10,1);
var结果=来自ct\U比率中的比率
其中rate.StartDate=日期
选择速率;
像这样的东西

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")

var result=from context.ct\u rate.Where(d=>d.start\u date>=“2010-10-01 00:00:00”和&d.end\u date只需使用普通的比较运算符

var result = ct_rates
    .Where(x => x.start_date <= myDate && x => x.endDate >= myDate);
var结果=ct\u费率
其中(x=>x.start\u date x.endDate>=myDate);

在我们的例子中,Robert的答案与我想要的非常接近。在我们的数据模型中,“结束”或“结束”日期列可以为空

需要类似以下内容:

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

当我只想比较数据部分时,我发现这是有效的

var results = from rate in ct_rates
              where rate.StartDate.Date <= date && rate.EndDate.Date >= date
              select rate;
var结果=来自ct\U费率中的费率
其中rate.StartDate.Date=日期
选择速率;