lambda今天和明天的条款

发布于 2025-02-08 17:09:00 字数 371 浏览 1 评论 0原文

我有一个LINQ查询,可与各种子句添加。 但是我也想退还单身人士参加今天或明天的活动。

Dances_Moderated = Dances_Moderated
  .Where(x => x.SingleDate == DateTime.Today) 
           && (x => x.SingleDate == DateTime.Today.AddDays(1))
  .ToList();

&& 后的代码以红色下划线,错误说:

CS0023:操作员'。不能应用于“ lambda表达式”类型的操作数

I have a Linq query that works with various Where clause additions.
But I also want to return the SingleDate for an Event where it is today or tomorrow.

Dances_Moderated = Dances_Moderated
  .Where(x => x.SingleDate == DateTime.Today) 
           && (x => x.SingleDate == DateTime.Today.AddDays(1))
  .ToList();

The code after the && is underlined in red, and the error says:

CS0023: Operator '.' cannot be applied to operand of type 'lambda expression'

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

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

发布评论

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

评论(2

清风无影 2025-02-15 17:09:00

您应该使用||),而不是&& and ):x 。我添加了.date为了处于安全方面:当我们使用==时,我们只想比较日期,而不是时间部分。

Dances_Moderated = Dances_Moderated
  .Where(x => x.SingleDate.Date == DateTime.Today || 
              x.SingleDate.Date == DateTime.Today.AddDays(1))
  .ToList();

You should use || (or) instead of && (and): x.SingleDate.Date should be either Today or tomorrow. I've added .Date in order to be on the safe side of the road: when we use == we want to compare dates only, not time parts.

Dances_Moderated = Dances_Moderated
  .Where(x => x.SingleDate.Date == DateTime.Today || 
              x.SingleDate.Date == DateTime.Today.AddDays(1))
  .ToList();
生死何惧 2025-02-15 17:09:00

您的代码中有几个错误:
第一:语法错误,
第二:业务错误。您的单身永远不会等于datetime..now。您应该使用更大和更少的操作员来做到这一点。

您的代码应该是这样:

Dances_Moderated = Dances_Moderated.Where(x => x.SingleDate >= DateTime.Today && x.SingleDate <= DateTime.Today.AddDays(1)).ToList();

You have couple of errors in your code:
First: Syntax error,
Second: Business error. Your SingleDate never gonna equal to datetime.now. You should use greater and less operator to do that.

Your code should be like this:

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