linq - Get the three latest time json elements -


i have json array. element format below:

{     "ckey": "s2_123_45_20130416105655",     "timeofcall": "2013-04-17t06:00:00-04:00",     "destinationnumber": "123456789",     "calltype": "x",     "cardnumber": "" } 

there many elements in array, want latest 3 order timeofcall(datetime format) within 24 hrs. may nothing or 1,2. maximum 3.
original thought using several dictionaries store , sort value complicated.

update:

this json array json string c# code:

datacontractjsonserializer ser = new datacontractjsonserializer(typeof(list<calldatadto>)); memorystream ms = new memorystream(encoding.utf8.getbytes(response4.content.readasstringasync().result)); calls = (list<calldatadto>)ser.readobject(ms); // dynamic jsonstr4 = _dynamicconvert.dynamictostring(calls);// convert dynamic string jsonarray4 = jarray.parse(jsonstr4); // convert string json array. 

update2:

?jsonarray4 {[    {     "ckey": "p2_123_23_20130416105655",     "timeofcall": "2013-04-17t06:00:00-04:00",     "destinationnumber": "1234567890",     "calltype": "debit",     "cardnumber": ""  },  { "ckey": "p5_123_105_20130412154035", "timeofcall": "2013-04-17t07:00:00-04:00", "destinationnumber": "1234567890", "calltype": "debit", "cardnumber": "" }, {   "ckey": "p5_123_114_20130412154000",   "timeofcall": "2013-04-07t08:00:00-04:00",   "destinationnumber": "1234567890",   "calltype": "debit",   "cardnumber": "" } ]} base {newtonsoft.json.linq.jcontainer}: {[ {   "ckey": "p2_123_23_20130416105655",   "timeofcall": "2013-04-17t06:00:00-04:00",   "destinationnumber": "1234567890",   "calltype": "debit",   "cardnumber": "" }, {   "ckey": "p5_123_105_20130412154035",   "timeofcall": "2013-04-17t07:00:00-04:00",   "destinationnumber": "1234567890",   "calltype": "debit",   "cardnumber": "" }, {  "ckey": "p5_123_114_20130412154000",  "timeofcall": "2013-04-07t08:00:00-04:00",  "destinationnumber": "1234567890",  "calltype": "debit",  "cardnumber": "" } ]}  type: array 

i try like

jsonarray4.where(item => convert.todatetime(item["timeofcall"]).date == datetime.now.date) .orderbydescending(item => convert.todatetime(item["timeofcall"])) .take(3); 

update: if need last 24 hours instead of current day than:

jsonarray4.where(item => convert.todatetime(item["timeofcall"]) >= datetime.now.addhours(-24)) .orderbydescending(item => convert.todatetime(item["timeofcall"])) .take(3); 

linq json

update2: suggested should work. here complete code sample:

string jsontext = @"[ {     'ckey': 'p2_123_23_20130416105655',     'timeofcall': '2013-04-17t06:00:00-04:00',     'destinationnumber': '1234567890',     'calltype': 'debit',     'cardnumber': '' },     {     'ckey': 'p5_123_105_20130412154035',     'timeofcall': '2013-04-17t07:00:00-04:00',     'destinationnumber': '1234567890',     'calltype': 'debit',     'cardnumber': '' }, {     'ckey': 'p5_123_114_20130412154000',     'timeofcall': '2013-04-07t08:00:00-04:00',     'destinationnumber': '1234567890',     'calltype': 'debit',     'cardnumber': '' }     ]";  var result = jarray.parse(jsontext).     where(item => convert.todatetime(item["timeofcall"]).date == new datetime(2013, 4, 17))     .orderbydescending(item => convert.todatetime(item["timeofcall"]))     .take(3); 

note have changed condition return something. if need results last 24 hours change condition >= datetime.now.addhours(-24)


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -