Reputation: 717
I'm trying to count objects in a list. My code looks like this:
var count = tempMessages
.Where(MessageDate => MessageDate > DateTime.Now.AddSeconds(-6))
.Count();
As you can see, I'm trying to count Messages where the MessageDate is max 6 seconds old. But I am getting this error message :
Operator '>' cannot be applied to operands of type 'ChatProj.Models.Message' and 'System.DateTime'
Upvotes: 2
Views: 968
Reputation: 1064
Because tempMessages is a list of type 'ChatProj.Models.Message and not DateTime, possibly there is a Date Property on Message so you can write this code:
var count = tempMessages.Where(Message => Message.Date > DateTime.Now.AddSeconds(-6)).Count();
Upvotes: 2
Reputation: 64487
tempMessages
is an enumerable of a custom class Message
, the Where
class will take that as the parameter to the expression, so you need to access the date property within it:
There is also an overload on Count
that can take an expression to save doing the Where
: http://msdn.microsoft.com/en-us/library/bb535181.aspx
var checkDate = DateTime.Now.AddSeconds(-6);
var count = tempMessages
.Count(message => message.Date > checkDate);
The error message highlights this, you are trying to say "is my message greater than this date", instead of "is the date of this message greater than this date".
Upvotes: 3