I'm trying to create an API method, which will allow to filter entries by date. I want to let to use two parameters - startDate and endDate. The second of them optional.
public IEnumerable<Recommendation> GetRecommendationByDate(DateTime startDate, DateTime? endDate)
{
if (endDate == null)
{
endDate = DateTime.Now;
}
var output = db.Recommendations.Where(r => r.IsPublished == true &&
r.CreatedDate.CompareTo(startDate) > 0 &&
r.CreatedDate.CompareTo(endDate) < 0)
.ToList();
return output;
}
After I've added nullable sign, method thows an exception when the second parameter (endDate) isn't null. When it is null, there is not any problems.
Exception sounds:
Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.
What is the reason and how to solve it?