Linq2SQL 两者之间

发布于 2024-08-22 13:31:14 字数 324 浏览 5 评论 0原文

是否可以在 Linq to SQL 中的 SQL 中执行此操作?

Select field from table where date between '2010-01-01' and '2010-01-31';

我意识到我可以做到:

where (monthBeginDate < a.StopDateActual && a.StopDateActual < monthEndDate)

但我很好奇我是否可以做到前者。我有一个坏习惯,就是在这样的陈述中搞砸小于和大于。

Is is possible to do this in SQL in Linq to SQL?

Select field from table where date between '2010-01-01' and '2010-01-31';

I realize I could do:

where (monthBeginDate < a.StopDateActual && a.StopDateActual < monthEndDate)

But I was curious if I could do the former. I have a bad habit of screwing up the less than and greater than on statements like that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落画卷 2024-08-29 13:31:14

您可以执行以下操作:

public bool Between(DateTime value, DateTime from, DateTime To) {
   return value > from && value < to;
}

where Between(a.StopDateActual, monthBeginDate, monthEndDate)

但是,您应该注意,这适用于 IEnumerable 而不是 IQueryable,即结果将在应用 where 之前从数据源中提取。在大量数据的情况下,这可能是一个性能问题,因此,您的 where 子句是您能做的最好的...只需小心 >且< !!!

You could do the following:

public bool Between(DateTime value, DateTime from, DateTime To) {
   return value > from && value < to;
}

where Between(a.StopDateActual, monthBeginDate, monthEndDate)

However, you should note that this works on IEnumerable and not on IQueryable, i.e. the results will be pulled from the data source before the where is applied. This could be a performance issue in the case of a large ammount of data, therefore, your where clasue is the best you can do ... just be careful with the > and < !!!

薄暮涼年 2024-08-29 13:31:14

不,这就是这样做的方法。我相信 C# 没有 Between 运算符。

你总是可以像我一样作弊并编写一个扩展方法:

public static bool BetweenDates (this DateTime checker, DateTime floor, DateTime ceiling)
{
    return (checker <= ceiling) && (checker >= floor);
}

然后你可以这样做;-)

.Where(s => s.StopDateActual.BetweenDates(monthBeginDate, monthEndDate));

Nope, that's the way to do it. C# does not have a between operator I believe.

You could always cheat and write an extension method like I do:

public static bool BetweenDates (this DateTime checker, DateTime floor, DateTime ceiling)
{
    return (checker <= ceiling) && (checker >= floor);
}

Then you can do this ;-)

.Where(s => s.StopDateActual.BetweenDates(monthBeginDate, monthEndDate));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文