Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to query data, where the result is in descending order. When I query -- startDate=04/09/2014&endDate=04/14/2014, I get results showing from the 9th instead the 14th. I am little unclear how is this achievable. This is what I currently have:

Updated Code:
C#
public HttpResponseMessage Get([FromUri] Query query)
        {
                var data = db.database_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<database_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);
                }
                int total = data.Count();

		data = data.OrderByDescending(c => c.UploadDate).Take(10);

                if (filteredData.Count != 0)
                {
                    data = data.OrderByDescending(d => d.UploadDate).Take(pageSize);
                }

                //return Request.CreateResponse(HttpStatusCode.OK, data);
                return Request.CreateResponse(HttpStatusCode.OK, new { total, data });
            }


Any advice on this would be very much appreciated. Many thanks.
Posted
Updated 17-Apr-14 1:36am
v2

1 solution

if (query.price_type != null)
{
data = data.OrderByDescending(d => d.UploadDate).Take(10);
}

if (query.endDate != null)
{
data = data.OrderByDescending(c => c.UploadDate <= query.endDate).Take(10);
}

if (query.startDate != null)
{
data = data.OrderByDescending(c => c.UploadDate >= query.startDate).Take(10);

}
if (filteredData.Count != 0)
{
data = data.OrderByDescending(d => d.UploadDate).Take(10);
}

replace that with below code.

data = data.OrderByDescending(c => c.UploadDate).Take(10);

Because you have already added filters at the top of the method. SO you should not do it again in the order by.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900