0

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?

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
magos
  • 3,371
  • 4
  • 29
  • 54
  • Why was this tagged `asp.net-mvc` ? Where do you get that exception? – Lasse V. Karlsen Jul 31 '14 at 11:02
  • Info:when you say second is optional.why did not you use Optional Parameter?public IEnumerable GetRecommendationByDate(DateTime startDate, DateTime endate=DateTime.Now); Something like that ? – Rangesh Jul 31 '14 at 11:06
  • Try changing `r.CreatedDate.CompareTo(endDate)` to `r.CreatedDate.CompareTo(endDate.Value)`. – Şafak Gür Jul 31 '14 at 11:07
  • @restless - it dosen't work because of another exception. See here: http://stackoverflow.com/questions/18740421/default-parameter-for-value-must-be-a-compile-time-constant – magos Jul 31 '14 at 11:16

1 Answers1

1

Add .HasVale and check if endDate contains the value or not and if it does then use as endDate.Value as shown below

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.HasValue ? endDate.Value : (The default you want to put when endDate is null)) < 0)
                                       .ToList();

            return output;
        }
Neel
  • 11,625
  • 3
  • 43
  • 61
  • I'm sorry, but this answer is still unclear for me. When I used this (putting Datetime.Now in indicated place), invoking without the second param, returned error: "No action was found on the controller 'RecApi' that matches the request." What's more - breakpoint in this action method was not even fired... – magos Jul 31 '14 at 13:00
  • I would like to allow users to invoke it this way: https://localhost:44301/api/recapi/getrecommendationbydate?startDate=2013-03-12 But it seems I can only do it as below https://localhost:44301/api/recapi/getrecommendationbydate?startDate=2013-03-12&endDate Can I fix it? – magos Jul 31 '14 at 13:05