Home > Net >  Lambda Where clause for Today and Tomorrow Dates
Lambda Where clause for Today and Tomorrow Dates

Time:06-20

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'

CodePudding user response:

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();

CodePudding user response:

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();
  • Related