从linq的第一个列表中仅填充第二个列表中当前月份的最近6个月数据

时间:2017-03-11 05:31:38

标签: c# linq

public void onActivityResult(int requestCode,int resultCode,Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CONTACT_PICK_REQUEST && resultCode == RESULT_OK){

        ArrayList<Contact> selectedContacts = data.getParcelableArrayListExtra("SelectedContacts");

        String display="";
        for(int i=0;i<selectedContacts.size();i++){
            display += (i+1)+"   "+selectedContacts.get(i).toString()+"\n";
            theNumber=selectedContacts.get(i).phone;
        }
        contactsDisplay.setText("Selected Contacts : \n\n"+display);
    }
}

protected void sendMsg(String theNumber, String myMsg) {
    String SENT = "Message Sent";
    String DELIVERED = "Message Delivered";

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

    registerReceiver(new BroadcastReceiver() {
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:

                        Toast.makeText(MainActivity.this, "SMS SENT", Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(MainActivity.this, "Generic Failure", Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(MainActivity.this, "No Service", Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    registerReceiver(new BroadcastReceiver() {

        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_LONG).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_LONG).show();
                    break;
            }

        }
    }, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(theNumber, null, myMsg, sentPI, deliveredPI);
}

(在此列表中,我的最高日期是2017年5月,所以从2017年5月起的最后6个月将是2016年12月,因为2016年12月不在列表中我必须跳过属于2016年11月的第一条记录,我必须填写下面提到的另一个列表。最后两个记录是4月和5月。

我试过这样但是这不起作用 - &gt;

List<MonthList> oldMonthResult = new List<MonthList>()
{
    new MonthList { Months = "nov", yearMonth ="nov"+DateTime.Now.AddYears(-1).Year, year=2017,datetime=Convert.ToDateTime("1/11/2016"),noofelevator=1},
    new MonthList { Months = "apr", yearMonth ="apr"+DateTime.Now.Year, year=2017,datetime=Convert.ToDateTime("1/4/2017"),noofelevator=3},
    new MonthList { Months = "may", yearMonth ="may"+DateTime.Now.Year, year=2017,datetime=Convert.ToDateTime("1/05/2017"),noofelevator=4},

};

我希望我的输出像这样 - &gt;

List<MonthList> only6monthdata = new List<MonthList>();
        only6monthdata = oldMonthResult.Select(x => new MonthList { noofelevator = x.noofelevator, yearMonth = x.yearMonth }).OrderByDescending(x => x.datetime.AddMonths(-6)).ToList();

1 个答案:

答案 0 :(得分:0)

您的查询无法正常工作的原因是您设置datetime=Convert.ToDateTime("1/11/2016")时,它的日期为1,月份为11,这显然是从今天起的最后6个月内。

您可以将日期设置为DateTime datetime = new DateTime(year, month, day);

您可以尝试以下查询,

DateTime latestDateTime = oldMonthResult.OrderByDescending(x => x.datetime).Select(x => x.datetime).FirstOrDefault();

only6monthdata = oldMonthResult.Where(x => x.datetime >= latestDateTime.AddMonths(-6)).Select(x => new MonthList { noofelevator = x.noofelevator, yearMonth = x.yearMonth }).OrderByDescending(x => x.datetime).ToList();