2

有一个日期参考 UserJoinDate 和日期列表 List friendsJoined 我想计算有多少朋友在 UserJoinDate 之前加入了一项服务,以及有多少朋友在同一时间或之后加入。

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

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

(该值从 Date: {9.11.2010 0:00:00} Day: 9 DayOfWeek: Tuesday DayOfYear: 313 复制而来...)

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

谢谢!

4

3 回答 3

1

尝试像这样分别使用 DateTime 的年、月和日属性进行比较;

FriendsJoinedBeforeCount = joinersDates .Where(x => (x.Year < UserJoinDate.Year) && (x.Month< UserJoinDate.Month) && (x.Day < UserJoinDate.Day)).Count(); 
于 2012-06-15T05:36:43.257 回答
1

您的问题似乎是 Visual Studio 的限制,而不是代码中的错误。Visual Studio 2010 不支持在其即时或监视窗口中定义 lambda 表达式。

如果您真的想在调试时测试 LINQ 表达式,您应该将它封装到一个方法中并从立即窗口调用它。

例如:

private int GetFriendsJoinedBeforeCount(DateTime joinDate)
{
    List<DateTime> joinersDates = getJoinersDates();
    return joinersDates.Count(x => x < UserJoinDate);
}

然后,在您的即时窗口中,您将调用:

GetFriendsJoinedBeforeCount(UserChurnDate)
于 2012-06-15T05:57:47.137 回答
0

试试下面的代码:

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

或者尝试使用 Ticks 属性:

List<DateTime> joinersDates = getJoinersDates();
FriendsJoinedBeforeCount = joinersDates.Count(x => x.ToUniversalTime().Ticks < UserJoinDate.ToUniversalTime().Ticks);
FriendsJoinedAfterCount = joinersDates.Count - FriendsJoinedBeforeCount ;
于 2012-06-15T05:32:33.420 回答