orderbydescending date linq查询 [英] orderbydescending date linq query

查看:66
本文介绍了orderbydescending date linq查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查询数据,结果按降序排列。当我查询 - startDate = 04/09 / 2014& endDate = 04/14/2014时,我得到的结果显示从第9个而不是第14个。我不清楚这是如何实现的。这就是我目前所拥有的:



更新代码:

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:

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.

推荐答案

if(query.price_type!= null)

{

data = data。 OrderByDescending(d => d.UploadDate)。取(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)。取(10);



}

if(filteredData.Count != 0)

{

data = data.OrderByDescending(d => d.UploadDate)。取(10);

}



用以下代码替换它。



data = data.OrderByDescending(c => c.UploadDate)。取(10);



因为您已经在方法的顶部添加了过滤器。因此,您不应该按顺序再次执行此操作。
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.


这篇关于orderbydescending date linq查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆