Search code examples
linqlambdawhere-clause

Lambda Where clause for Today and Tomorrow Dates


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'


Solution

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