Reputation: 478
I have developed a web api
which takes two parameters of date time (starting and end), and then should give distinct records.
public HttpResponseMessage GetMeterPing(DateTime start, DateTime end)
{
try
{
var startDateTime = start;
var endDateTime = end;
var result = medEntitites.tj_xhqd.Where(m => m.sjsj >= startDateTime && m.sjsj <= endDateTime)
.OrderByDescending(o => o.sjsj)
.Select(s => new { s.zdjh, s.sjsj, s.xhqd })
.Distinct()
.FirstOrDefault();
return Request.CreateResponse(HttpStatusCode.OK, new { data = result });
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
}
API URL: http://localhost:14909/api/meters/GetMeterPing/2018-04-28T00:00:00/2018-04-27T23:59:59
When I run this web-api
it gives me
{"data":null}
Also while debugging it the result
is also null
Any help would be highly appreciated
Upvotes: 0
Views: 1040
Reputation: 3154
Your description of your problem sounds like the Linq query you are executing is not returning any results, so your call to FirstOrDefault
is defaulting, which is to say it is returning null. You then perform no additional validations, and respond with the result of this line set as the value of an anonymous projection having a data
property.
Upvotes: 5