C#linq查询排名由两个条件组成

时间:2016-02-17 06:11:53

标签: c# linq

使用C#linq查询, 我想获得按ID和更新时间排名的日期。 id:包含在列表'idList'中 updatetime:datatime的类型

示例数据:

查询:

.onecenter{text-align:center;width:150px;}
<table  border="1" cellspacing="0" cellpadding="0">
    <tr >
        <td class='onecenter'>id</td>
        <td class='onecenter'>updatetime</td>
    </tr>
    <tr >
        <td class='onecenter'>1</td>
        <td class='onecenter'>2016-02-16</td>
    </tr>
    <tr >
        <td class='onecenter'>2</td>
        <td class='onecenter'> 2016-02-14</td>
    </tr>
    <tr>
        <td  class='onecenter'>3</td>
        <td  class='onecenter'>2016-02-15</td>
    </tr>
    <tr>
        <td class='onecenter'>4</td>
        <td  class='onecenter'>2016-02-17</td>
    </tr>
</table>

IDLIST:

2,3

结果应为:

    .onecenter{text-align:center;width:150px;}
    <table  border="1" cellspacing="0" cellpadding="0">
        
        <tr >
            <td class='onecenter'>id</td>
            <td class='onecenter'>updatetime</td>
        </tr>
       <tr>
            <td  class='onecenter'>3</td>
            <td  class='onecenter'>2016-02-15</td>
        </tr>
        <tr >
            <td class='onecenter'>2</td>
            <td class='onecenter'> 2016-02-14</td>
        </tr>
        <tr>
            <td class='onecenter'>4</td>
            <td  class='onecenter'>2016-02-17</td>
        </tr>
        <tr >
            <td class='onecenter'>1</td>
            <td class='onecenter'>2016-02-16</td>
        </tr>
        
       
    </table>

如何通过linq查询获得结果。

我试过了:

var data1 = query.Where(m => idList.Contains(m.ID)).OrderByDescending(x => x.updatetime);
var data2 = query.Where(m => !idList.Contains(m.ID)).OrderByDescending(x => x.updatetime);
var date = data1.Concat(data2).ToList();

和:

var data = query.GroupBy(m => idList.Contains(m.ID)).OrderByDescending(x => x.updatetime);

但它没有得到正确的结果。

2 个答案:

答案 0 :(得分:2)

我会使用此查询:

var result = query.OrderByDescending(q => idList.Contains(q.ID) ? 1 : 0)
                  .ThenByDescending(q => q.updatetime);

答案 1 :(得分:1)

尝试使用:

Except

使用data1为第二个删除[2016-02-17 06:22:04.167 UTC] 3 2/15/2016 12:00:00 AM [2016-02-17 06:22:04.169 UTC] 2 2/14/2016 12:00:00 AM [2016-02-17 06:22:04.170 UTC] 4 2/17/2016 12:00:00 AM [2016-02-17 06:22:04.170 UTC] 1 2/16/2016 12:00:00 AM 中已有的内容,然后您就可以获得此内容:

IEnumerable

修改

如果您的数据很多,您可以考虑将项目保留为List,直到您需要处理它而不是将其更改为var data1 = query.Where(x => idList.Contains(x.ID)).OrderByDescending(y => y.UpdateTime); var data2 = query.Except(data1).OrderByDescending(y => y.UpdateTime); var data = data1.Concat(data2); //don't change to List here foreach(var dat in data){ //use it by fetching it one by one like this } 。这可能会节省一些时间:

    List<String> messagelist = new ArrayList<>();

    try
    {
        AmazonSQS sqs = new AmazonSQSClient(credentials);
        Region usWest2 = Region.getRegion(Regions.US_WEST_2);
        sqs.setRegion(usWest2);
        boolean flag = true;

        while(flag)
        {
            ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queuename);
            receiveMessageRequest.setMaxNumberOfMessages(number_of_message_);
            receiveMessageRequest.withMaxNumberOfMessages(number_of_message_).withWaitTimeSeconds(wait_time_second_);
            List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();

            for (Message message : messages) 
                  {
                    //   System.out.println("    Body:          " + message.getBody());
                       messagelist.add( message.getBody());

                       String messageReceiptHandle = message.getReceiptHandle();
                       sqs.deleteMessage(new DeleteMessageRequest().withQueueUrl(queuename).withReceiptHandle(messageReceiptHandle));
                    }
            if(messages.size()==0)
            {
                flag = false;
            }
        }         

    }
    catch (AmazonServiceException ase) {
        ase.printStackTrace();
    } catch (AmazonClientException ace) {
       ace.printStackTrace();
    }
    finally {
        return messagelist ;
    }