c# - 如何将日期时间变量与 lambda 表达式进行比较?

标签 c# .net

有一个日期引用 UserJoinDate 和日期列表 List FriendsJoined 我想计算有多少 friend 在 UserJoinDate 之前加入了服务,以及有多少 friend 在同一时间或之后加入。

我写了以下几行,但它不起作用:

List<DateTime> joinersDates = getJoinersDates();
FriendsJoinedBeforeCount = joinersDates .Where(x => x < UserJoinDate).Count();
FriendsJoinedAfterCount = joinersDates .Where(x => x >= UserJoinDate).Count();

有谁知道如何计算 joinersDates 列表中有多少日期在 UserJoinDate 之前以及有多少相同或之后?

编辑

这是来自 ImmediateWindow 的副本:

churnersDates.Count( x => x > UserChurnDate)
Expression cannot contain lambda expressions

UserJoinDate 是日期时间

示例:

UserJoinDate = 9.11.2010 0:30:00

(该值复制自日期:{9.11.2010 0:00:00} 天:9 星期几:星期二 年中某日:313...)

joinersDates.First() = 17.5.2011 0:30:00

谢谢!

最佳答案

尝试分别使用 DateTime 的 Year、Month 和 Day 属性进行比较,如下所示;

FriendsJoinedBeforeCount = joinersDates .Where(x => (x.Year < UserJoinDate.Year) && (x.Month< UserJoinDate.Month) && (x.Day < UserJoinDate.Day)).Count(); 

关于c# - 如何将日期时间变量与 lambda 表达式进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11044957/

相关文章:

c# - 查询 WMI 类 Win32_QuickFixEngineering 返回许多奇怪的时间戳 (C#)

c# - 无法通过 Style 为 ListBox 设置 GroupStyle?

c# - 使用C#更改excel中一个单元格的字体大小

c# - C#/VB.NET 中的缩略图

c# - 当 DatagridviewComboBox 选定索引更改时更改 DatagridviewTextBox 文本

c# - 为什么一路往下用Async/await

c# - 当工厂方法返回 null 时,我应该抛出什么异常?

C#:HashSet的volatile读写

c# - 等待的网络流信号发送数据

c# - ASP.NET HeadLoginView 注销 单击。也退出其他本地网站