简体   繁体   English

如何使用orderbydescending在linq中查询?

[英]How to query in linq using orderbydescending?

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. 我得到的结果显示从9日而不是16日。 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. 请指教。

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. 首先,如果您对列表重新排序4次,前3个将是浪费时间。 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; 其次,传递给OrderByDescendingFunc获取按顺序排序的 ; 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. 如果您的上传日期在查询开始日期之后,则使用true键,如果在查询开始日期之前,则使用false So you're going to get all of the true s followed by all of the false s. 因此,您将获得所有true s,然后是所有的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 . 这将选择上载日期在查询开始日期之后的所有项目, 然后通过UploadDate对其进行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). 我的建议是首先使用Where应用每个过滤器,然后通过执行OrderByDescending(c => c.UploadDate) (或您计划订购的任何属性OrderByDescending(c => c.UploadDate)在最后执行OrderByDescending(c => c.UploadDate)

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. 如果你可以扩展一点,我可以进一步帮助。

你试试这个吗?

data.Where(c => c.UploadDate >= query.startDate).OrderByDescending(c => c.UploadDate)

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM