Reputation: 620
I am trying to query data, where the result is in descending order. When I query --
StartDate=04/09/2014&EndDate=04/16/2014,
I get results showing from the 9th instead the 16th. I am little unclear how is this achievable. This is what I currently have:
public HttpResponseMessage Get([FromUri] Query query)
{
var data = db.data_bd.AsQueryable();
int pageSize = 10;
if (query.price_type != null)
{
data = data.Where(c => c.Cover == query.price_type);
}
if (query.endDate != null)
{
data = data.Where(c => c.UploadDate <= query.endDate);
}
if (query.startDate != null)
{
data = data.Where(c => c.UploadDate >= query.startDate);
}
// If any other filters are specified, return records which match any of them:
var filteredData = new List<IQueryable<data_bd>>();
if (!string.IsNullOrEmpty(query.name))
{
var ids = query.name.Split(',');
foreach (string i in ids)
{
filteredData.Add(data.Where(c => c.Name != null && c.Name.Contains(i)));
}
}
if (filteredData.Count != 0)
{
data = filteredData.Aggregate(Queryable.Union);
}
if (!data.Any())
{
var message = string.Format("No data was found");
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
}
if (query.startDate != null)
{
data = data.OrderByDescending(c => c.UploadDate >= query.startDate).Take(pageSize);
}
if (filteredData.Count != 0)
{
data = data.OrderByDescending(d => d.UploadDate).Take(pageSize);
}
//return Request.CreateResponse(HttpStatusCode.OK, data);
}
Am I missing something? Please advice.
Upvotes: 2
Views: 126
Reputation: 25221
You're a little bit off here. First of all, if you reorder the list 4 times, the first 3 will have been a waste of time. I'm not sure what your logic is there, so I'll leave it at that. You need to rethink your filtering logic.
Secondly, the Func
that you pass to OrderByDescending
gets the key to order by; it doesn't perform filtering. So when you do:
data = data.OrderByDescending(c => c.UploadDate >= query.startDate)
You are using a key of true
if your upload date is after the query start date and false
if it's before the query start date. So you're going to get all of the true
s followed by all of the false
s. You're not ordering by date at all.
To order by date, you need to do something like this:
data.Where(c => c.UploadDate >= query.startDate)
.OrderByDescending(c => c.UploadDate)
This selects all of the items where the upload date is after the query start date and then orders them by UploadDate
.
My advice would be to apply each of your filters first using Where
and then do your ordering at the end by doing OrderByDescending(c => c.UploadDate)
(or whatever property you plan to order by).
Upvotes: 5
Reputation: 13495
I think you are probably trying to filter between those dates and then order by descending date. Try this:
if (query.endDate != null)
{
data = data.Where(c => c.UploadDate <= query.endDate).Take(pageSize);
}
if (query.startDate != null)
{
data = data.Where(c => c.UploadDate >= query.startDate).Take(pageSize);
}
data = data.OrderByDescending(c => c.UploadDate);
Upvotes: 1
Reputation: 831
Have you try this
data.Where(c => c.UploadDate >= query.startDate).OrderByDescending(c => c.UploadDate)
Upvotes: 2
Reputation: 65077
You're not ordering by dates.. you're ordering by a boolean expression.
data = data.OrderByDescending(c => c.UploadDate <= query.endDate).Take(pageSize);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ bool
I can't quite understand what you're trying to achieve there.. but that's your issue. If you can expand a little more I can help further.
Upvotes: 3